[英]What does `${1+/$1}` mean in Bash?
I was reading a Bash script with this line:我正在用这一行阅读 Bash 脚本:
FOO=${1+/$1}
What does this line do?这条线有什么作用?
The /
makes it look slightly more confusing than it is, but this is just an example of ${foo+bar}
, which expands to bar
if $foo
is set. /
使它看起来比实际更混乱,但这只是${foo+bar}
的一个示例,如果设置$foo
,它将扩展为bar
。
In this case, the variable is $1
, the first positional parameter passed to the script or function.在这种情况下,变量是
$1
,传递给脚本的第一个位置参数或 function。
example () {
echo "${1+/$1}"
}
example # outputs nothing
example '' # outputs "/"
example foo # outputs "/foo"
There is a table that summarises these parameter expansions in the spec.有一个表格总结了规范中的这些参数扩展。 The rules for
${parameter+word}
are: ${parameter+word}
的规则是:
word
word
word
word
So to answer your question directly, FOO=${1+/$1}
assigns /$1
to FOO
is $1
is set, otherwise FOO
is set to null (an empty string).因此,要直接回答您的问题,
FOO=${1+/$1}
将/$1
分配给FOO
是$1
设置,否则FOO
设置为 null (一个空字符串)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.