Variable definition in 'sh -c'

sh -c 'TMP=??; echo $TMP;'

When using double quotes the parameter expansion occurs when the command line is built i.e. the shell does not see

TMP=??; echo $TMP;

as its parameter but

TMP=??; echo ;

if $TMP is empty in the calling shell environment.


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 echo).

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"'

Tags:

Shell

Variable