简体   繁体   English

echo $ {1 +“ $ @”}是什么意思

[英]what does echo ${1+“$@”} mean

From /usr/local/bin/erl 从/ usr / local / bin / erl

ROOTDIR=/usr/local/lib/erlang
BINDIR=$ROOTDIR/erts-5.9.1/bin
EMU=beam
PROGNAME=`echo $0 | sed 's/.*\///'`
export EMU
export ROOTDIR
export BINDIR
export PROGNAME
exec $BINDIR/erlexec ${1+"$@"}

I know "$@" meams arguments. 我知道“ $ @”表示争论。 But {1+"$@"} means what? 但是{1 +“ $ @”}是什么意思?

From IEEE Std 1003.1 (POSIX 2013), Shell command language : 从IEEE Std 1003.1(POSIX 2013)开始, Shell命令语言

${parameter:+[word]}

Use Alternative Value. 使用替代值。 If parameter is unset or null, null shall be substituted; 如果参数未设置或为null,则应替换为null。 otherwise, the expansion of word (or an empty string if word is omitted) shall be substituted. 否则,将替换单词的扩展名(如果省略单词,则为空字符串)。

Ie, ${1+"$@"} expands to the value of "$@" , the command line arguments, except when $1 is not set, ie there are no command line arguments, in which case the expression expands to nothing. 即, ${1+"$@"}扩展为命令行参数"$@"的值,除非未设置$1 ,即没有命令行参数,在这种情况下表达式扩展为空。 A simpler script that shows how this works is 一个更简单的脚本显示了它是如何工作的是

echo '"' ${1+"$@"} '"'

If you store this in a file test.sh and run it, you get: 如果将其存储在文件test.sh并运行它,则会得到:

/tmp$ sh test.sh
" "
/tmp$ sh test.sh 1
" 1 "
/tmp$ sh test.sh 1 2
" 1 2 "

(The spaces at the begin and end come from echo .) (开头和结尾的空格来自echo 。)

larsmans provides an explanation of the semantics, but doesn't clarify why ${1+"$@"} is different than simply "${@}" . larsmans提供了语义的解释,但是没有阐明为什么${1+"$@"}与简单的"${@}" In a properly behaving shell, "${@}" expands to nothing. 在行为正常的shell中,“ $ {@}”扩展为空。 That is: foo "$@" should call foo with no arguments if "$@" is empty. 也就是说:如果“ $ @”为空,则foo "$@"应该不带任何参数调用foo In an incorrectly behaving shell, foo "$@" will be invoked with one argument (the empty string). 在行为不正确的shell中,将使用一个参数(空字符串)调用foo "$@" Many historical shells expanded "$@" to the empty string rather than to nothing, but ${1+"$@"} correctly expands to nothing. 许多历史Shell都将“ $ @”扩展为空字符串,而不是扩展为空,但是${1+"$@"}正确扩展为零。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM