Bash – What does this line in bash do? Parameter-||scriptname

bashparametershell-script

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! 🙂

Best Answer

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
  ___________________|___________________|_________________|________________
Related Question