Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

In bash:

$ type :
: is a shell builtin
$ type true
true is a shell builtin

Looks like they are the same, but they don't give the same system trace:

$ strace :
strace: :: command not found
$ strace true
execve("/bin/true", ["true"], [/* 82 vars */]) = 0
[snip]
exit_group(0)                           = ?

I tried diffing strace bash -c : 2>:.txt and strace bash -c true 2>true.txt, but couldn't find any differences between them except for the memory locations.

In dash:

$ type :
: is a special shell builtin
$ type true
true is a shell builtin

OK, so they are not the same. help : and help true aren't very useful, and they return the same in bash and dash. Is there any practical difference at all between them, except that : saves three bytes and makes scripts less readable?

share|improve this question
2  
Related question about the purpose of the colon builtin. –  jw013 Mar 22 '12 at 2:35

2 Answers 2

up vote 20 down vote accepted

There's no real difference in behavior. Both commands do nothing and exit with a successful status. : emphasizes doing nothing; true emphasizes the successful status.

strace true works because true is both a shell builtin and an external command (/bin/true); : is only a shell builtin (there's no /bin/: -- though there could be, and probably was on very old Unix systems). In bash, try

type -a :
type -a true

The reasons that both exist are historical. If I recall correctly, some very early shells didn't have a comment syntax, so the do-nothing : command was used instead.

There is some internal difference in dash. Looking through the source, available at git://git.kernel.org/pub/scm/utils/dash/dash.git, shows some different code paths in eval.c, but I haven't been able to produce any visibly different behavior other than the word special in the output of type :.

share|improve this answer
7  
Additionally, early versions of UNIX did not have /bin/true or /bin/false. Also the : command is sometimes used for the argument processing side effects: : ${num_times:=10}. –  Arcege Mar 21 '12 at 18:16
3  
: was originally a label indicator, back in an ancestor of the Bourne shell that had goto. Apparently : was abused as a comment indicator and stuck. –  Gilles Mar 21 '12 at 23:40
    
The behavior of : as a label indicator for goto was preserved in Microsoft's pseudo-Unix command line clone, command.com, and remains in its successor cmd.exe, as does the practice of abusing :: for a line comment. –  Sorpigal Jun 25 '12 at 12:19

They're identical in Bash. Look at builtins/colon.def in the Bash-4.2 source code.

In your command strace true you are actually running the binary /bin/true instead of the bash built-in true.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.