6

I'm reading the scripts from here and trying to understand what's going on. This function performs changing the directory of a Finder window:

function ee { 
 osascript -e 'set cwd to do shell script "pwd"'\
 -e 'tell application "Finder"'\
 -e "if (${1-1} <= (count Finder windows)) then"\
 -e "set the target of window ${1-1} to (POSIX file cwd) as string"\
 -e 'else' -e "open (POSIX file cwd) as string"\
 -e 'end if' -e 'end tell';\
};\

I'm assuming the $ is interpreted by bash, since it's inside double-quotes. I haven't been able to find what could {1-1} mean. I've played with the expression in separate test scripts but couldn't find a difference from plain $1. Any ideas?

myxal
  • 87
  • 1
  • 7
  • 1
    I *believe* it's a "default". If the user supplied a command line argument then `${1-1}` would expand to that argument, otherwise it would simply expand/"default" to whatever was after the hypen -- in your case `1`. – jedwards Dec 30 '13 at 09:25

1 Answers1

13

This means that if argument 1 (${1}) is not set, it will be set to 1.

See parameter substitution here.

 ${parameter-default}, ${parameter:-default}
   If parameter not set, use default.
James Risner
  • 5,451
  • 11
  • 25
  • 47
RedX
  • 14,749
  • 1
  • 53
  • 76
  • 3
    +1, Just a note: I was suggested [here](http://stackoverflow.com/questions/20783187/remove-a-word-from-a-string-in-bash-shell-linux/20783245#comment31159567_20783245) that, "ABS is not a great reference -- it shows a lot of bad practices in its examples. Better to point folks at mywiki.wooledge.org/BashFAQ/073 " – anishsane Dec 30 '13 at 13:57
  • @anishsane thank you for the reference. The examples are really bad in tldp.org. But their layout is more concise IMHO. – RedX Dec 30 '13 at 15:22