1

What does VAR_NAME=${VAR_NAME:-"/some/path/file"} mean in an shell script?

This is for an init script, I'm writing a custom one to get some of our startup operations into init scripts so that we can start them automatically on boot, but I don't have much experience with shell scripting so I'm using a startup script for an unrelated piece of software that's we've customized in the past.

The path pointed to is to a file that contains configuration values that override defaults set in the script.

I'm having trouble figuring out what that construct really means (the :- part in particular).

The script I'm working off of also seems to chain this operation together to resolve which value to use such as:

LOG_FILE=${LOG_FILE:-${LOGFILE:-$DEFAULT_LOG_FILE}}

John
  • 5,166
  • 27
  • 31

2 Answers2

2
  ${parameter:-word}
        Use Default Values. If parameter is unset or null, the expansion
        of word shall be substituted; otherwise, the value of parameter shall be
        substituted.
William Pursell
  • 204,365
  • 48
  • 270
  • 300
  • The chain probably derives from a cut-n-paste in which the variable had a different name. It is a fairly common idiom to write `A=${B:-${C:-default}}` – William Pursell Jul 02 '12 at 19:37
  • The chaining is in case someone defined `LOGFILE` instead of `LOG_FILE`. If `LOG_FILE` is defined it uses that, otherwise if `LOGFILE` is defined it uses that, otherwise it uses `DEFAULT_LOG_FILE` – Gordon Davisson Jul 02 '12 at 19:53
1

It sets VAR_NAME equal to VAR_NAME if it exists or /some/path/file if it doesn't.

Chaining it would only make sense if the variable names were different going down the chain.

Conner
  • 30,144
  • 8
  • 52
  • 73
  • I misread the source actually they are different. The value used by the init script is `LOG_FILE` while the value in the configuration file is `LOGFILE`. I miscopied it – John Jul 02 '12 at 19:38