-7

Why do the following examples from Arrow's reply output $var instead of 3?

I single quote or backslash $var as '$var' or \$var, hoping that

  • first $var will be passed literally into the execution environment of the executable program /bin/echo, and
  • then inside the execution environment of the executable program /bin/echo, the environment variable var=3 applies when parameter expansion happens on $var.

Why doesn't the parameter expansion happen inside the execution environment of the executable program /bin/echo? Note:

  • in the original shell, $var is single quoted or backslashed, so should not be expanded in the execution environment of the original shell.

  • the original shell removes the single quotes or backslash around $var, and then passes $var and environment variable var=3 into the execution environment for running /bin/echo, so I think that parameter expansion of $var should happen in the execution environment for running /bin/echo.

Note that I use set -x to print out the trace information, but can't figure out what is actually the result after expansion but before execution.

Thanks.

tim$ unset var
$ set -x

tim$ var=3 /bin/echo '$var'
+ var=3
+ /bin/echo '$var'
$var

tim$ var=3 /bin/echo \$var
+ var=3
+ /bin/echo '$var'
$var

$ var=3 exec /bin/echo \$var | cat
+ cat
+ var=3
+ exec /bin/echo '$var'
$var

tim$ var=3 exec /bin/echo '$var' | cat
+ var=3
+ exec /bin/echo '$var'
+ cat
$var

closed as unclear what you're asking by Archemar, Stephen Rauch, dr01, Shadur, Rui F Ribeiro Aug 9 '17 at 16:35

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

5

In every single case, you are either strong-quoting, which prevents expansion of the variable; or escaping the $ character, which also makes it no longer a variable subject to expansion. /bin/echo and cat doesn't do any parameter or variable expansion whatsoever, so piping into it would not cause such to occur.

Not the answer you're looking for? Browse other questions tagged or ask your own question.