I tried to define a variable in a 'sh -c' command string:
sh -c "TMP=??; echo $TMP;"
Nothing was printed.
Why can't I define a variable in a 'sh -c' string?
sh -c 'TMP=??; echo "$TMP"'
With double quotes around the
sh -c code, the
$TMP is expanded by the interactive shell before the
sh -c code executes. With single quotes,
$TMP will be expanded inside the
sh shell. (I've also properly quoted the
$TMP variable expansion for
Single quotes protects a string from variable expansions.
If you do not intend the
?? to be treated as a filename globbing pattern inside the
sh -c script, then use
sh -c 'TMP="??"; echo "$TMP"'