gpt4 book ai didi

bash - :- and := in Bash parameter substitution?有什么区别

转载 作者:行者123 更新时间:2023-11-29 08:56:21 28 4
gpt4 key购买 nike

Bash 参数替换中的 :- 和 := 有什么区别?

他们似乎都设置了默认值?

最佳答案

引用 Bash Reference Manual :

${parameter:-word}

If parameter is unset or null, the expansion of word is substituted. Otherwise, the value of parameter is substituted.

${parameter:=word}

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.

区别在于 := 不仅替换了 word,它还将它分配参数:

var=
echo "$var" # prints nothing
echo "${var:-foo}" # prints "foo"
echo "$var" # $var is still empty, prints nothing
echo "${var:=foo}" # prints "foo", assigns "foo" to $var
echo "$var" # prints "foo"

看到这个很棒 wiki.bash-hackers.org tutorial获取更多信息。

关于bash - :- and := in Bash parameter substitution?有什么区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48218775/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com