庆典指定默认值 [英] bash assign default value

查看:132
本文介绍了庆典指定默认值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

$ {参数:=词}
                指定的默认​​值。如果参数没有设置或者为空,则
  字的扩张被分配给
  参数。参数的值是
  然后取代。阵地
  参数和特殊
                参数可能不以这种方式被分配给

${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.

这篇关于庆典指定默认值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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