If I type
::
into a bash shell, I get:
-bash: ::: command not found
But, only one :
results in no output. Why is this?
If I type
::
into a bash shell, I get:
-bash: ::: command not found
But, only one :
results in no output. Why is this?
The last colon is just part of the default "not found" message:
The reason a single colon produces nothing is that
:
is a valid command - although it does nothing (except returnTRUE
). From theSHELL BUILTIN COMMANDS
section ofman bash
:You will sometimes see it in constructions like
See for example What purpose does the colon builtin serve?
The
:
shell built-in vs non-existent::
The
:
shell built-in command exists (note the difference between external and built-in commands) which does nothing; it just returns success, just like thetrue
command. The:
built-in is standard and defined by the POSIX standard, where it's also known as the "null utility". It is frequently used for testing or for running infinite loops as inwhile : ; do ...;done
However,
::
- two colon characters together - are interpreted as one "word" to the shell, and it assumes to be a command the user entered. The shell will go through the process of checking built-ins, then any directory in thePATH
variable for existence of that command. But there is neither a built-in::
nor external command::
. Therefore, that produces an error.Well, what is a typical format for an error?
Thus, what you see isn't 3 colons but what you typed pasted into the standard error format.
Note also, that
:
can take command-line arguments, i.e. it is legal to do:In this case, the shell will consider that as two "words", one of which is a command and the other a positional parameter. That will also produce no error! (See also the historical note (later in this answer) about the use the of
:
with positional parameters.)In shells other than bash
Note that formatting can vary between different shells as well. For
bash
,ksh
, andmksh
the behavior is consistent. For instance, Ubuntu's default/bin/sh
shell (which is actually/bin/dash
):where 1 is the command number (equivalent to the line number in a script).
csh
by contrast produces no error message at all:In fact, if you run
strace -o csh.trace csh -c ::
, the trace output incsh.trace
file reveals thatcsh
exits with exit status 0 (no errors). Buttcsh
does output the error (without outputting its name, though):Error messages
In general, the first item in the error message should be the executing process or function (your shell tries to execute
::
, hence the error message comes from the shell). For instance, here the executing process isstat
:In fact, POSIX defines the perror() function, which according to the documentation takes a string argument, then outputs error message after colon, and then newline. Quote:
And the string argument to
perror()
technically could be anything, but of course for clarity it's typically the function name orargv[0]
.By contrast, GNU has its own set of functions and variables for error handling, which a programmer can use with
fprintf()
tostderr
stream. As one of the examples on the linked page shows, something like this could be done:Historical note
In old Unix and Thompson shell,
:
was used withgoto
statement (which according to user named Perderabo on this thread wasn't a shell built-in). Quote from the manual:So you could do something like this to make an infinite loop script:
Try any other non-existent command and you'll see that the
:
serves its normal purpose in English:the 3rd is a spacer from formatting
in bash a
:
is an empty line void instructionThe added colon is part of the error message itself. If one types
cd ow
it results inbash: cd: ow: No such file or directory
, which shows that the error is putting in the extra colon: No such file or directory
you get 3 colons because the error format contains a colon: