$ {参数:=词} 指定的默认值。如果参数没有设置或者为空,则 字的扩张被分配给 参数。参数的值是 然后取代。阵地 参数和特殊 参数可能不以这种方式被分配给
${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.
我想我可以使用这个功能来写 $ {LONG_VARIABLE_NAME:=你好} 而不是较长的 LONG_VARIABLE_NAME = $ {LONG_VARIABLE_NAME:-Hello } ,但现在的bash也试图执行'你好'这给了未找到命令。任何方式避免这种情况?或将我必须坚持后者?有人可以举一个例子,其中指定默认实际上是有用的?
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?
推荐答案使用冒号:
: ${A:=hello}结肠是空指令,什么也不做,而忽略它的参数。它内置的bash所以不会创建一个新的进程。
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.
更多推荐
庆典指定默认值
发布评论