awk '{print tolower($0)}'
prevents the shell from expanding $0
. Awk gets to see $0
.
awk "{print tolower($0)}"
makes the shell expand $0
to the name of the current script or shell, something like -bash
in an interactive session. Awk gets to see
print tolower(-bash)
and because it doesn't know about any variable called bash
, it initializes it to 0.
$(somecmd "${ROLE_NAME}" | someothercmd)
returns the output of
somecmd "${ROLE_NAME}" | someothercmd
and the output undergoes word splitting and glob expansion;
"$(somecmd "${ROLE_NAME}" | someothercmd)"
does the same, but word splitting and glob expansion don't take place.
However, if the command substitution is on the right-hand side of an assignment, globbing and word splitting are suppressed by default:
var=$(somecmd "${ROLE_NAME}" | someothercmd)
is equivalent to
var="$(somecmd "${ROLE_NAME}" | someothercmd)"
It doesn't hurt to quote, though.
Notice that the "suppressed by default" statement only applies for the right-hand side as a whole; within it, you still have to quote. This is important when dealing with arrays: observe
$ myfunc () { echo 1 2 3; } # Generates three words
$ arr=($(myfunc)) # Unquoted within ()
$ declare -p arr
declare -a arr='([0]="1" [1]="2" [2]="3")' # Three array elements
$ arr=("$(myfunc)") # Quoted within ()
$ declare -p arr
declare -a arr='([0]="1 2 3")' # One array element
And finally, "automatically quoted on the right-hand side applies only to expansions, i.e., var=$1
(no quotes needed) or var=$(command)
(no quotes needed around $()
; maybe needed within command
itself. It does not apply when the right hand side contains spaces: var=a b c
does not assign a b c
to var. It tries to set an environment variable var
to value a
, then run command b
with argument c
.