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*

4
  • It looks like this also works well without the export: (SUMAN_DEBUG=foo && echo $SUMAN_DEBUG) and keeps it in the scope of the command sequence Commented Mar 27, 2019 at 15:30
  • 3
    I'm afraid that's wrong. The first one doesn't work because command arguments are expanded before variable assignment happens. The second one works because SUMAN_DEBUG=foo performs an assignment in the scope of the current shell (my terminology may be imprecise here). @groovenectar's answer seems like a way to go. Your solution with export is useful if the variable is intended to be referenced from a subshell (e.g. you invoke a script that expects that variable to be set). Commented Jul 26, 2019 at 9:42
  • My answer is indeed wrong. Thanks for pointing it out. I re-ran my tests and realized that one of these forms sets the variable as a side-effect so that later tests showed it expanding because of leakage between test cases. I am still not satisfied with the generally accepted answer because I routinely pass variables to apps this way and it works. I wonder if echo being a built-in results in slightly different precedence rules being invoked. Commented Jul 27, 2019 at 15:06
  • This answer should either be updated or removed. Commented Sep 29, 2021 at 10:57