Shell – Why is there a number in the zsh parameter expansion ${1-$PWD}

shellvariable substitutionzsh

I have this script I'm basing my current script off of. I just don't understand why he has typeset result part dir=${1-$PWD} in there.

I get the same result if I just write dir=$PWD. With typeset is ${1-$PWD} changing how dir is set vs $PWD?

Best Answer

${1-$PWD} is a shell parameter expansion pattern.

It is used to expand to a default value based on another -- whatever on the right of -. Here, in your case:

  • If $1 is unset, then the expansion of $PWD would be substituted

  • Otherwise i.e. if $1 is set to any value (including null), its value would be used as the result of expansion

Example:

% echo "Foo${1-$PWD}"   
Foo/home/bar

% set -- Spam

% echo "Foo${1-$PWD}"
FooSpam
Related Question