问题描述
我搜索了空操作在bash(:),但无法找到任何有用信息。这是什么经营者的具体目的或用途呢?
I searched for noop in bash (:), but was not able to find any good information. What is the exact purpose or use case of this operator?
我试过以下,它的工作是这样对我来说:
I tried following and it's working like this for me:
[mandy@root]$ a=11
[mandy@root]$ b=20
[mandy@root]$ c=30
[mandy@root]$ echo $a; : echo $b ; echo $c
10
30
请让我知道,该运营商实时或者它是强制性使用它的任何地方的用例。
Please let me know, any use case of this operator in real time or any place where it is mandatory to use it.
推荐答案
它的存在,更由于历史的原因。结肠内置:
完全等同于真正
。这是传统的方法是使用真正
当返回值是很重要的,例如在一个无限循环:
It's there more for historical reasons. The colon builtin :
is exactly equivalent to true
. It's traditional to use true
when the return value is important, for example in an infinite loop:
while true; do
echo 'Going on forever'
done
这是传统方法是使用:
当shell语法需要一个命令,但是你什么都没有做。
It's traditional to use :
when the shell syntax requires a command but you have nothing to do.
while keep_waiting; do
: # busy-wait
done
的:
内置的历史可以追溯到汤普森壳,这是中的。 :
是汤普森shell的转到
语句的标签指示。标签可以是任何文字,所以:
弯了腰为注释指示器(如果没有转到评论
,然后:注释
是一个有效的注释)。该没有转到
但保留:
The :
builtin dates all the way back to the Thompson shell, it was present in Unix v6. :
was a label indicator for the Thompson shell's goto
statement. The label could be any text, so :
doubled up as a comment indicator (if there is no goto comment
, then : comment
is effectively a comment). The Bourne shell didn't have goto
but kept :
.
一个使用常见的成语:
是的,这台 VAR
到值
如果是未设置并执行咱这 VAR
已被设置。这种结构只存在于一个变量替换的形式,而这个变量替换需要以某种方式命令的一部分:无操作指令提供很好
A common idiom that uses :
is : ${var=VALUE}
, which sets var
to VALUE
if it was unset and does nothing if var
was already set. This construct only exists in the form of a variable substitution, and this variable substitution needs to be part of a command somehow: a no-op command serves nicely.
这篇关于什么是空操作,用例[:]在bash?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!