Home > Blockchain >  What does the colon in ": exit 0" do?
What does the colon in ": exit 0" do?

Time:08-09

I have a bash script which ends with:

: exit 0

I understand that the colon is a bash built-in NOP command.

What is the point of passing it an argument of exit 0?

CodePudding user response:

The : builtin just ignores any arguments, so it's essentially just a way to comment except the arguments will be expanded. So in many cases the result might be different from a simple comment

: whatever command arguments can follow the ":"
: this will have side $(echo effect >file.txt)

That means in this case it's just useless. The return value is the same in both : exit 0 and exit 0

From the SHELL BUILTIN COMMANDS section in man bash

: [arguments]:

No effect; the command does nothing beyond expanding arguments and performing any specified redirections. The return status is zero.

It has some other useful purposes that you can read in What is the purpose of the : (colon) GNU Bash builtin?, for example to begin a block comment

  • Related