冒号运算符的说明及QUOT;:$ {富=值}" [英] Explanation of colon operator in ": ${foo=value}"

查看:166
本文介绍了冒号运算符的说明及QUOT;:$ {富=值}"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据我所知在bash,它如同空,我知道这是在参数扩展使用,以及使用其他方式,但有人可以解释这个

I understand the colon operator in bash that acts like a null, and I know it's used in parameter expansion, as well as being used other ways, but can someone explain this:

: ${SOMETHING='value'}

从实验我知道,这个设置环境变量 $ SOMETHING '值'但是为什么呢?

就因为它是一个有效的答案,但再请点我为此使用它的文档(这我似乎无法找到)或正确的名称将是有益的。我希望有一个虽然更富有启发性的解释。

"Just because it does" is a valid answer but then please point me to the documentation for it (which I can't seem to find) or a proper name for this usage would be useful. I'm hoping there's a more enlightening explanation though.

推荐答案

这位前pression设置的东西 如果尚未设置值。这是一个有用的操作者在许多情况下。然而,它也返回指定的值,所以如果你只是执行

The expression sets SOMETHING to value if it isn't already set. This is a useful operator to have in many situations. However, it also returns the assigned value, so if you simply executed

${SOMETHING='value'}

那么你的shell会尝试调用命令。这可能会或可能不会做不必要的东西;在至少它会抛出一个消息值:找不到命令。

then your shell would try to invoke the command value. This might or might not do something unwanted; at the least it would throw a message "value: command not found".

要避免这一点,你可以使用无操作,它计算它的参数,然后它扔了出去,而不是执行它

To avoid this you can use the no-op :, which evaluates its argument and then throws it away, rather than executing it.

这篇关于冒号运算符的说明及QUOT;:$ {富=值}"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆