Monday, August 10, 2009

Another Use For The Colon (:) Command

I've already blogged about the shell's no-op command, when I use it, and why.

I forgot about another way I use it. Here 'tis.

Consider this loop:
for i in {1..100}
do

some-command-or-other $i
done
If you want to comment out the loop, you could comment out the whole thing by putting a comment character in front of each line, but that's tedious. Easier would be to comment out the one-line loop body, like this
for i in {1..100}
do

# some-command-or-other $i
done
Except, um, that's an empty loop body and a syntax error.

bash: syntax error near unexpected token `done'
This, however, is fine:
for i in {1..100}
do

: some-command-or-other $i
done
That loop body isn't empty, it just doesn't do anything. No fuss, no muss.

No comments: