Search code examples
bashzshparameter-expansion

Parameter expansion without colon (default value to empty string) in a condition


I know the difference between ${var:-word} and ${var-word}, but I don't see the purpose of using a default value in this condition:

[ -z "${NVM_PROFILE-}" ]
  • Why is an empty default value needed there?
  • Is it something related to compatibility with other shells (zsh)?

Solution

  • I like set -u.

    $ sh -c '[ -z "NVM_PROFILE" ] && echo empty'
    empty
    $ sh -c 'set -u; [ -z "$NVM_PROFILE" ] && echo empty'
    sh: NVM_PROFILE: unbound variable
    $ sh -c 'set -u; [ -z "${NVM_PROFILE-}" ] && echo empty'
    empty