In the answer to this question about comments in shell scripting, it is indicated that the : is a null command that explicitly does nothing (but is not to be used for comments).
What would be the utility of a command that does absolutely nothing?
|
In the answer to this question about comments in shell scripting, it is indicated that the What would be the utility of a command that does absolutely nothing? |
|||
|
I typically use
The one place I've found that
|
|||||||||||
|
|
This will run forever unless |
|||
|
|
In addition to using it as a statement that does nothing, you can use it to comment out single statements by turning them into arguments for :. |
|||
|
When you want an "unless" statement in shell scripting, you either use a "not" condition, which can look goofy for some of the tests, or you use ':' in the true-clause, with real code in the false-clause.
The "exotic condition" could be something you don't want to negate, or that's just a lot clearer if you don't use "negative logic". |
|||||||||||||
|
|
Originally, it was used to determine that it was a Bourne shell program, as opposed to C compiled program. This was before shebang and multiple scripting languages (csh, perl). You can still run a script starting with just
It will generally run the script against Since then, the main use is to evaluate the arguments. I still use:
to set a default value in a script. |
|||
|
|
|
I've only ever used this in addition to the # character for temporarily commenting out a line, in a situation in which commenting out the line produces a syntax error, due to a defect in the shell grammar of not allowing an empty sequence of commands:
Without the : we have a missing command sequence, which is a syntax error. |
|||
|
|
|
In the old pre-bourne shell in ancient versions of UNIX, the |
|||
|
|
|
There are two cases where I find Default variable assignments
This is a convenient way to allow users of your shell script to override a setting without editing the script. (However, command-line arguments are better because you don't run the risk of unexpected behavior if the user coincidentally has the variable you use in their exported environment.) Here's how the user would override the setting:
The Even though It would also be acceptable to use The following script would also work:
But the above is much harder to maintain. If a line using Something to put in an empty conditional blockEmpty conditional blocks should generally be avoided, but they're sometimes useful:
Some people argue that having an empty true
is arguably easier to read than:
However I believe there are a few alternative approaches that are better than an empty true block:
|
|||
|
|
:is required to be a built-in, whiletrueis not, which affects the scope of variables – Old Pro Apr 28 '12 at 0:33