What are : at the beginning of lines used for in a Bash script?
I have seen some usage of : at the beginning of lines, like in the following snippet of code:
cgcreate -g "$cgroups:/$uuid"
: "${BOCKER_CPU_SHARE:=512}" && cgset -r cpu.shares="$BOCKER_CPU_SHARE" "$uuid"
: "${BOCKER_MEM_LIMIT:=512}" && cgset -r memory.limit_in_bytes="$((BOCKER_MEM_LIMIT * 1000000))" "$uuid"
Experiments show that : functions much like a comment (#), since nothing is printed to stdout; however, : $(echo foo > bar) creates a
new file bar, so code execution does happen.
:is a null command, but shell evaluates its args before doing nothing with them. However, $( .. ) is aprocess expansion-- the...contents are executed to provide the arg before it is discarded. This is a known code injection attack route. – Paul_Pedant Mar 08 '21 at 16:57