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.
Two of mine.
Embed POD comments
A quite funky application of
:
is for embedding POD comments in bash scripts, so that man pages can be quickly generated. Of course, one would eventually rewrite the whole script in Perl ;-)Run-time function binding
This is a sort of code pattern for binding functions at run-time. F.i., have a debugging function to do something only if a certain flag is set:
You get:
Somewhat related to this answer, I find this no-op rather convenient to hack polyglot scripts. For example, here is a valid comment both for bash and for vimscript:
Sure, we may have used
true
just as well, but:
being a punctuation sign and not an irrelevant English word makes it clear that it is a syntax token.As for why would someone do such a tricky thing as writing a polyglot script (besides it being cool): it proves helpful in situations where we would normally write several script files in several different languages, with file
X
referring to fileY
.In such a situation, combining both scripts in a single, polyglot file avoids any work in
X
for determining the path toY
(it is simply"$0"
). More importantly, it makes it more convenient to move around or distribute the program.A common example. There is a well-known, long-standing issue with shebangs: most systems (including Linux and Cygwin) allow only one argument to be passed to the interpreter. The following shebang:
will fire the following command:
and not the intended:
Thus, you would end up writing a wrapper script, such as:
This is where polyglossia enters the stage.
A more specific example. I once wrote a bash script which, among other things, invoked Vim. I needed to give Vim additional setup, which could be done with the option
--cmd "arbitrary vimscript command here"
. However, that setup was substantial, so that inlining it in a string would have been terrible (if ever possible). Hence, a better solution was to write it in extenso in some configuration file, then make Vim read that file with-S "/path/to/file"
. Hence I ended up with a polyglot bash/vimscript file.It's there more for historical reasons. The colon builtin
:
is exactly equivalent totrue
. It's traditional to usetrue
when the return value is important, for example in an infinite loop:It's traditional to use
:
when the shell syntax requires a command but you have nothing to do.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'sgoto
statement. The label could be any text, so:
doubled up as a comment indicator (if there is nogoto comment
, then: comment
is effectively a comment). The Bourne shell didn't havegoto
but kept:
.A common idiom that uses
:
is: ${var=VALUE}
, which setsvar
toVALUE
if it was unset and does nothing ifvar
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.See also What purpose does the colon builtin serve?.