What is the purpose of a command that does nothing, being little more than a comment leader, but is actually a shell builtin in and of itself?
It's slower than inserting a comment into your scripts by about 40% per call, which probably varies greatly depending on the size of the comment. The only possible reasons I can see for it are these:
# poor man's delay function
for ((x=0;x<100000;++x)) ; do : ; done
# inserting comments into string of commands
command ; command ; : we need a comment in here for some reason ; command
# an alias for `true' (lazy programming)
while : ; do command ; done
I guess what I'm really looking for is what historical application it might have had.
You could use it in conjunction with backticks (
``
) to execute a command without displaying its output, like this:Of course you could just do
some_command > /dev/null
, but the:
-version is somewhat shorter.That being said I wouldn't recommend actually doing that as it would just confuse people. It just came to mind as a possible use-case.
If you'd like to truncate a file to zero bytes, useful for clearing logs, try this:
Self-documenting functions
You can also use
:
to embed documentation in a function.Assume you have a library script
mylib.sh
, providing a variety of functions. You could either source the library (. mylib.sh
) and call the functions directly after that (lib_function1 arg1 arg2
), or avoid cluttering your namespace and invoke the library with a function argument (mylib.sh lib_function1 arg1 arg2
).Wouldn't it be nice if you could also type
mylib.sh --help
and get a list of available functions and their usage, without having to manually maintain the function list in the help text?A few comments about the code:
declare -f
to enumerate all available functions, then filters them through sed to only display functions with the appropriate prefix.mylib.sh function1
and it gets translated internally tolib_function1
. This is an exercise left to the reader.$1
. At the same time, it will clutter your namespace if you source the library. If you don't like that, you can either change the name to something likelib_help
or actually check the args for--help
in the main code and invoke the help function manually.I saw this usage in a script and thought it was a good substitute for invoking basename within a script.
... this is a replacement for the code:
basetool=$(basename $0)
:
can also be for block comment (similar to /* */ in C language). For example, if you want to skip a block of code in your script, you can do this: