6

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?

6

${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
7

That's not brace expansion, that's a standard parameter expansion operator (dates back to the Bourne shell in the 70s).

${1-$PWD}

Expands to the value of $1 (the first positional parameter) if it is set (if $# is strictly greater than 0) even to the empty string, or to the content of the $PWD variable otherwise.

Run:

info zsh 'Parameter Expansion'

for details.

typeset is not Bourne nor POSIX, but it's not zsh-specific either. It comes from the Korn shell (from the early 80s) and is used to limit the scope of a variable to the current function. It's also found in bash and yash.

Run:

info zsh typeset

for details.

4

It tests $1 for a value, using that before $PWD.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.