In the Bourne shell and derivatives like Bash, :
is a no-op command: that is, it doesn't do anything, but arguments are evaluated normally. Contrast this with a comment (#
), which does nothing at all (everthing following the #
is simply ignored).
This syntax:
: ${POSTGRES_USER:=postgres}
Is commonly used to assign default values to variables. The syntax ${POSTGRES_USER:=postgres}
(a) will set the variable POSTGRES_USER
to postgres
only if it does not already have a value, and will then (b) evaluate to the value of $POSTGRES_USER
. If we used a comment:
# ${POSTGRES_USER:=postgres}
...nothing at all would happen, because the shell simply ignores the entire line. Using :
means that the ${...}
expression is still evaluated, so POSTGRES_USER
is assigned a value if necessary.
Update
If there was no :
at the beginning of the line, then this:
${POSTGRES_USER:=postgres}
would be a shell command! The shell would first evaluate the variable expansion and come up with something like postgres
, so you would have a shell script that effectively looked like this:
postgres
Which would probably give you the error:
bash: postgres: command not found...