3

I've been reading through the bash man page, and reading through scripts on my system (CentOS 6.7), looking up things in the bash man page as I go. It's a great exercise; I learned, for instance, how /etc/profile checks if the -i option is set when there are actually no options in the positional parameters (so getopts wouldn't work).

However, the following line has me totally stumped; I can't find anything in the bash man page that explains what it could be doing:

LESSOPEN="${LESSOPEN-||/usr/bin/lesspipe.sh %s}"

(This is a portion of a line in /etc/profile.d/less.sh.)

Am I just missing something in man bash?


Yes, I was missing something in man bash: Above the explanation for ${parameter:-word}, it says Omitting the colon results in a test only for a parameter that is unset. This was the missing piece. (And is not covered in the "possible duplicate" question, by the way.)

The fact that the default value being assigned was the name of a script after an "or" operator only added to my confusion! :)

Wildcard
  • 36,499

1 Answers1

7

This is not bash specific but it existed in the Bourne Shell since 1976.

Check the Bourne Shell man page:

http://schillix.sourceforge.net/man/man1/bosh.1.html

Check section Parameter Substitution currently starting on page 7.

${parameter-word}        Use Default Values. If parameter is unset,
                         the  expansion  of  word is substituted;
                         otherwise,  the  value of parameter is substituted.

For a complete overview, there is:

                     | parameter nonnull | parameter null  | parameter unset
  ___________________|___________________|_________________|________________
  ${parameter:-word} | subst. parameter  | subst. word     | subst. word
  ___________________|___________________|_________________|________________
  ${parameter-word}  | subst. parameter  | subst. null     | subst. word
  ___________________|___________________|_________________|________________
  ${parameter:=word} | subst. parameter  | assign word     | assign word
  ___________________|___________________|_________________|________________
  ${parameter=word}  | subst. parameter  | subst. null     | assign word
  ___________________|___________________|_________________|________________
  ${parameter:?word} | subst. parameter  | error, exit     | error, exit
  ___________________|___________________|_________________|________________
  ${parameter?word}  | subst. parameter  | subst. null     | error, exit
  ___________________|___________________|_________________|________________
  ${parameter:+word} | subst. word       | subst. null     | subst. null
  ___________________|___________________|_________________|________________
  ${parameter+word}  | subst. word       | subst. word     | subst. null
  ___________________|___________________|_________________|________________
schily
  • 19,173
  • Aha. man bash covers the :- usage, but doesn't document the - usage. And I see from the above that they are different. Good to know. So then, the rest of the line is used as just a literal string (if it's used at all)—i.e. '||/usr/bin/lesspipe.sh %s' with no expansions of any kind? – Wildcard Oct 16 '15 at 12:39
  • Whoops, it IS covered after all! Omitting the colon results in a test only for a parameter that is unset. Thanks very much! – Wildcard Oct 16 '15 at 12:42
  • 1
    The table above was taken from the POSIX standard. The original Bourne Shell man page did not document the difference between :- and - very well although it is hidden in some sentence. It seems that bash copied from the original Bourne Shell man page and introduced a similar hard to understand text. – schily Oct 16 '15 at 12:43
  • Yes, you found the sentence taken from the old Bourne Shell man page! – schily Oct 16 '15 at 12:44
  • Yes—thanks again! Actually, I'm relieved to know that the crazy-long bash man page IS complete after all; it was worrying to think I could read the whole thing and still be in mystery about certain features. :) – Wildcard Oct 16 '15 at 12:48
  • 1
    BTW: If I remember a recent talk from Steven Bourne correctly, the support for the additional colon was added around 1979. – schily Oct 16 '15 at 12:55