2

I came across this construct while reviewing some older unix shell scripts, what does it mean, and why is is used?

${1+"$@"}
xxfelixxx
  • 6,512
  • 3
  • 31
  • 38

2 Answers2

2

I found this explanation from unix haters handbook, page 152 of text (page 190 of the pdf). http://web.mit.edu/~simsong/www/ugh.pdf

It’s the way to exactly reproduce the command line arguments in the /bin/sh family of shells shell script.

It says, “If there is at least one argument ( ${1+ ), then substitute in all the arguments ( “$@” ) preserving all the spaces, etc. within each argument.

If we used only “$@” then that would substitute to “” (a null argument) if there were no invocation arguments, but we want no arguments reproduced in that case, not “”.

Why not “$*” etc.? From a sh(1) man page:

Inside a pair of double quote marks (“”), parameter and command substitution occurs and the shell quotes the results to avoid blank interpretation and file name generation. If $* is within a pair of double quotes, the positional parameters are substituted and quoted, separated by quoted spaces (“$1 $2 …”); however, if $@ is within a pair of double quotes, the positional parameters are substituted and quoted, separated by unquoted spaces (“$1” “$2” …).

I think ${1+“$@”} is portable all the way back to “Version 7 Unix.”

Wow! All the way back to Version 7.

xxfelixxx
  • 6,512
  • 3
  • 31
  • 38
0

Google should be your best friend! This is a similar questions asked almost 2 years ago.

https://unix.stackexchange.com/questions/68484/what-does-1-mean-in-a-shell-script-and-how-does-it-differ-from

Community
  • 1
  • 1
Lottamus
  • 457
  • 4
  • 7