Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

16
  • 2
    What about /usr/bin/env? that doesn't work either, do you know why? Commented Jul 9, 2014 at 16:43
  • 9
    The same reason - the shell expands the $TEST before the command line is executed. Once the echo is running (also note that echo will usually translate to the shell built-in command and not to /bin/echo) it sees the variable set in its environment. However, echo $TEST doesn't tell echo to output the contents of variable TEST from its environment. It tells the shell to run echo with argument being whatever currently is in the variable called TEST - and those are two very different things. Commented Jul 9, 2014 at 17:41
  • 2
    As I explained to you here: it’s because variables are expanded inside double quotes (e.g., "… $var …") but not inside single quotes (e.g., '… $var …').  Since echo "$var" is inside single quotes, that entire string gets passed to the new (sh -c) shell without being interpreted by the outer, interactive shell. … (Cont’d) Commented Mar 14, 2018 at 20:21
  • 1
    @MuhammadUmer because it is defined that way - check the link I've added to the answer. Commented Jan 23, 2021 at 18:56
  • 1
    using two commands would work, but the reason why one simple command does not output foo IMO explained incorrectly, another answer is much better: unix.stackexchange.com/a/56454/266260: echo is usually a built-in and is not executed, var=value sh -c 'echo "$var"' works as one would expect. Commented Jan 21, 2022 at 3:41