A shell colon does nothing. Use it anyway

58 points by refp


hayalci

I had learned this trick years ago, if you have your prompt like this, you can quickly copy paste an entire line and it'll execute the command, ignoring the prompt bits.

: hayalci@tassadar ~/src/dool ; jj rebase -s dxw -d mrn

mort

I often use colons in Makefiles for pretty printing. You might be tempted to write Makefiles like this:

out/%.o: src/%.c
    @echo CC $@
    @$(CC) -c -o $@ $<

However I've had issues in the past where make spawns multiple echo process at a time which interfere with each other, since they're writing to the same stream. You could write this instead:

out/%.o: src/%.c
    : CC $@
    @$(CC) -c -o $@ $<

and now it's the make process itself that does the printing rather than a child process.

spillybones

Ooh, this is a nice explanation. I saw this trick recently in a shell script, but didn't have time to look into how it works. It is also unfortunately very hard to find documentation, since man : or whatis : turn up nothing useful.

I suppose it's kinda like the true command, except that it definitely doesn't have special behavior for --help/--version/etc and it's fewer characters.

Student

Why do you need the null command to force parameter expansion? Doesn’t it happen without the colon?