0

While looking at the apache run script /etc/init.d/apache2 I realise something different in String Manipulating for Substring Extraction. For example:

if [ "${APACHE_CONFDIR##/etc/apache2-}" != "${APACHE_CONFDIR}" ] ; then

or

if [ -n "${PIDTMP:-}" ] && kill -0 "${PIDTMP:-}" 2> /dev/null; then

The second one seems Use Default Values if PIDTMP is unset or null but I didn't find it logical in this one.

What is the use of minus sign (-) in each case? Brief explanation would be helpful.

Note:I am interested in minus signs only in braces and I already checked :-word .

Chris Maes
  • 35,025
  • 12
  • 111
  • 136
Fredrick Gauss
  • 5,126
  • 1
  • 28
  • 44
  • 1
    Those two are different. The first one strips `/etc/apache2-` from the front of `$APACHE_CONFDIR`, the second one defaults to empty string, which seems to be pretty much useless (`"${PIDTMP}"` is the empty string when `PIDTMP` isn't set). – dhke Aug 25 '15 at 07:12
  • Sometimes redundant additions confuse people, but thanks for your comment :) – Fredrick Gauss Aug 25 '15 at 07:30

1 Answers1

1

It seems to me that this has no real effect. :- is supposed to mean: "if not set, use the following default value", but since nothing follows, afaik it has no effect:

  • if the variable is set, then default value will NOT be taken
  • if the variable is unset, the it will expand to "", which would have happened anyway when using "${PIDTMP}".

The only use case I can imagine is to make sure that the expansion contains at least the empty string and not an "unset variable" (this makes a difference when the -u option is in effect).

In the bash man page:

${parameter:-word} Use Default Values. If parameter is unset or null, the expansion of word is substituted. Otherwise, the value of parameter is substituted.

and

-u Treat unset variables and parameters other than the special parameters "@" and "*" as an error when performing parameter expansion. If expansion is attempted on an unset variable or parameter, the shell prints an error message, and, if not interactive, exits with a non-zero status.

Toby Speight
  • 27,591
  • 48
  • 66
  • 103
Chris Maes
  • 35,025
  • 12
  • 111
  • 136
  • Thank you. I would expecting logical answer, but it seems how it is. Maybe it is there for historical concerns. – Fredrick Gauss Aug 25 '15 at 07:27
  • yes, any script can have "historical leftovers" :) – Chris Maes Aug 25 '15 at 08:29
  • the `-n` option of bash works also for unset parameters, so the script would do exactly the same if we removed the :- everywhere. – Chris Maes Aug 25 '15 at 08:30
  • 1
    There is a difference between `${foo:-}` and `${foo}`, when bash option `-u` is set - the former is never an error. – Toby Speight Aug 25 '15 at 08:39
  • @TobySpeight do you have any reference for this `bash -u` option? I think that might cover the case I described, where it matters for bash if a variable is unset or empty string. – Chris Maes Aug 25 '15 at 08:47
  • @Chris - yes, that's the error-on-unset flag. See the docs for `set` in the SHELL BUILTIN COMMANDS section of the manual page. – Toby Speight Aug 25 '15 at 08:51
  • `-u Treat unset variables as an error when substituting.` so without `:-` gives error now as `bash: foo: unbound variable`. Thank you @TobySpeight – Fredrick Gauss Aug 25 '15 at 08:53