As I read through an installation bash script I stumbled across this line:
: ${SUDO:=sudo}
"${SUDO}" apt install vim
I know that SUDO is set to sudo if SUDO was not set before.
But what does the colon at the beginning do here?
As I read through an installation bash script I stumbled across this line:
: ${SUDO:=sudo}
"${SUDO}" apt install vim
I know that SUDO is set to sudo if SUDO was not set before.
But what does the colon at the beginning do here?
: is a command that does nothing - ignores its arguments and any input, and exits with zero exit status. See posix colon or man colon.
It's typically used in endless loops:
while :; do
in empty expressions:
if something; do
: do nothing
done
to trigger some debugging output:
cmd=(strange command)
set -x
: "${cmd[@]}" # set -x will show
for commenting stuff out temporary and for multiline comments with a here document:
: <<EOF
A multiline comment.
EOF
or as in your question to use ${...:=...} expansion to assign some default value to a variable if the variable is not set. Note that you should quote the expansion, so that filename expansion does not happen, because filename expansion in such expressions may cause your script to magically lag out of no reason in some extreme cases.
# SUDO="${SUDO:+sudo}" <- you have to type SUDO twice :(
: "${SUDO:=sudo}" # <- only type SUDO once!