bash 分配默认值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4437573/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
bash assign default value
提问by zedoo
${parameter:=word} Assign Default Values. If parameter is unset or null, the expansion of word is assigned to parameter. The value of parameter is then substituted. Positional parameters and special parameters may not be assigned to in this way.
${parameter:=word} 分配默认值。如果参数未设置或为空,则将单词的扩展分配给参数。然后替换参数的值。不能以这种方式分配位置参数和特殊参数。
I thought I could use this feature to write ${LONG_VARIABLE_NAME:=hello}
instead of the longer LONG_VARIABLE_NAME=${LONG_VARIABLE_NAME:-hello}
, but now bash also tries to execute 'hello' and that gives a command not found. Any way to avoid that? Or will I have to stick to the latter? Can someone give an example where the assign default is actually useful?
我以为我可以使用此功能来编写${LONG_VARIABLE_NAME:=hello}
而不是更长的LONG_VARIABLE_NAME=${LONG_VARIABLE_NAME:-hello}
,但是现在 bash 也尝试执行 'hello' 并且给出了未找到的命令。有什么办法可以避免吗?还是我必须坚持后者?有人可以举一个例子,其中分配默认值实际上有用吗?
回答by camh
Use a colon:
使用冒号:
: ${A:=hello}
The colon is a null command that does nothing and ignores its arguments. It is built into bash so a new process is not created.
冒号是一个空命令,它什么都不做并且忽略它的参数。它内置于 bash 中,因此不会创建新进程。
回答by Jonathan L
Please look at http://www.tldp.org/LDP/abs/html/parameter-substitution.htmlfor examples
有关示例,请查看http://www.tldp.org/LDP/abs/html/parameter-substitution.html
${parameter-default}, ${parameter:-default}
If parameter not set, use default. After the call, parameter is still not set.
Both forms are almost equivalent. The extra :
makes a difference only when parameter has been declared, but is null.
如果未设置参数,则使用默认值。调用后,参数仍未设置。
这两种形式几乎是等价的。:
仅当参数已声明但为空时,额外的才会有所不同。
unset EGGS
echo 1 ${EGGS-spam} # 1 spam
echo 2 ${EGGS:-spam} # 2 spam
EGGS=
echo 3 ${EGGS-spam} # 3
echo 4 ${EGGS:-spam} # 4 spam
EGGS=cheese
echo 5 ${EGGS-spam} # 5 cheese
echo 6 ${EGGS:-spam} # 6 cheese
${parameter=default}, ${parameter:=default}
If parameter not set, set parameter value to default.
Both forms nearly equivalent. The : makes a difference only when parameter has been declared and is null
如果未设置参数,则将参数值设置为默认值。
两种形式几乎相同。: 仅当参数已声明且为空时才有所作为
# sets variable without needing to reassign
# colons suppress attempting to run the string
unset EGGS
: ${EGGS=spam}
echo 1 $EGGS # 1 spam
unset EGGS
: ${EGGS:=spam}
echo 2 $EGGS # 2 spam
EGGS=
: ${EGGS=spam}
echo 3 $EGGS # 3 (set, but blank -> leaves alone)
EGGS=
: ${EGGS:=spam}
echo 4 $EGGS # 4 spam
EGGS=cheese
: ${EGGS:=spam}
echo 5 $EGGS # 5 cheese
EGGS=cheese
: ${EGGS=spam}
echo 6 $EGGS # 6 cheese
${parameter+alt_value}, ${parameter:+alt_value}
If parameter set, use alt_value, else use null string. After the call, parameter value not changed.
Both forms nearly equivalent. The : makes a difference only when parameter has been declared and is null
如果设置了参数,则使用 alt_value,否则使用空字符串。调用后,参数值没有改变。
两种形式几乎相同。: 仅当参数已声明且为空时才有所作为
unset EGGS
echo 1 ${EGGS+spam} # 1
echo 2 ${EGGS:+spam} # 2
EGGS=
echo 3 ${EGGS+spam} # 3 spam
echo 4 ${EGGS:+spam} # 4
EGGS=cheese
echo 5 ${EGGS+spam} # 5 spam
echo 6 ${EGGS:+spam} # 6 spam
回答by SiegeX
The default value parameter expansion is often useful in build scripts like the example one below. If the user just calls the script as-is, perl will not be built in. The user has to explicitly set WITH_PERL
to a value other than "no" to have it built in.
默认值参数扩展在构建脚本中通常很有用,例如下面的示例。如果用户只是按原样调用脚本,则不会内置 perl。用户必须显式设置WITH_PERL
为“no”以外的值才能内置它。
$ cat defvar.sh
#!/bin/bash
WITH_PERL=${WITH_PERL:-no}
if [[ "$WITH_PERL" != no ]]; then
echo "building with perl"
# ./configure --enable=perl
else
echo "not building with perl"
# ./configure
fi
Build withoutPerl
不用Perl构建
$ ./defvar.sh
not building with perl
Build withPerl
使用Perl构建
$ WITH_PERL=yes ./defvar.sh
building with perl