The Unappreciated Bourne Shell ":" Operator
Some people think that the Bourne shell's : is a comment character. It isn't, really. It evaluates its arguments and returns a zero exit status (). Here are a few places to use it:
- Replace the UNIX true command to make an endless while loop (). This is more efficient because the shell doesn't have to start a new process each time around the loop (as it does when you use
while true):
while : do
commandsdone(Of course, one of the
commandswill probably be break, to end the loop eventually.) - When you want to use the else in an if (), but leave the then empty, the
:makes a nice "do-nothing" place filler:
if
somethingthen : elsecommandsfi
- If your Bourne shell doesn't have a true
#comment character, you can use:to "fake it." It's safest to use quotes so the shell won't try to interpret characters like>or|in your "comment":
: 'read answer and branch if < 3 or > 6'
- Finally, it's useful with parameter substitution () like
${var?}or${var=default}. For instance, using this line in your script will print an error and exit if either the USER or HOME variables aren't set:
: ${USER?} ${HOME?}
- JP