Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
661 views
in Technique[技术] by (71.8m points)

shell - ${1:+"$@"} in /bin/sh

I've noticed that sometimes wrapper scripts will use ${1:+"$@"} for the parameters rather than just "$@".

For example, http://svn.macosforge.org/repository/macports/trunk/dports/editors/vim-app/files/gvim.sh uses

exec "$binary" $opts ${1:+"$@"}

Can anyone break ${1:+"$@"} down into English and explain why it would be an advantage over plain "$@"?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

'Hysterical Raisins', aka Historical Reasons.

The explanation from JesperE (or the Bash man page on shell parameter expansion) is accurate for what it does:

  • If $1 exists and is not an empty string, then substitute the quoted list of arguments.

Once upon 20 or so years ago, some broken minor variants of the Bourne Shell substituted an empty string "" for "$@" if there were no arguments, instead of the correct, current behaviour of substituting nothing. Whether any such systems are still in use is open to debate.

[Hmm: that expansion would not work correctly for:

command '' arg2 arg3 ...

In this context, the correct notation is:

${1+"$@"}

This works correctly whether $1 is an empty argument or not. So, someone remembered the notation incorrectly, accidentally introducing a bug.]


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.7k users

...