Timeline for Setting an environment variable before a command in Bash is not working for the second command in a pipe
Current License: CC BY-SA 4.0
15 events
when toggle format | what | by | license | comment | |
---|---|---|---|---|---|
Jun 5, 2023 at 12:43 | comment | added | 0xC0000022L |
@rampion you sure? echo after all, is likely the shell builtin, even in ZSh. So in order to set an actual environment variable you'd still have to export it into the environment. Otherwise it remains a shell variable of some scope (here likely global), but child processes won't see it (terms and conditions apply when we're talking about subshells, though ;)).
|
|
Nov 17, 2022 at 23:37 | history | edited | starball | CC BY-SA 4.0 |
add link to Keith's user profile.
|
Oct 18, 2020 at 8:04 | comment | added | Honghao Z |
If you want to set multiple env, you can run (export FOO1='bar1'; export FOO2='bar2'; command...)
|
|
Jun 20, 2020 at 3:39 | comment | added | jsejcksn |
I encountered this error (and fix) in zsh : unix.stackexchange.com/questions/208607/…
|
|
Nov 25, 2018 at 17:59 | comment | added | Dennis Williamson | One circumstance in which a variable assignment would fail and you might not want the command to be executed is if the variable was previously marked read-only. | |
Apr 13, 2017 at 22:11 | comment | added | weberc2 |
This is the only answer that works for evaluating the same variables in the command. For example, (export FOO=foo && echo "$FOO/bar/baz/$FOO") .
|
|
Jun 9, 2016 at 2:05 | comment | added | danecando |
I have a bash script with a bunch of environment variables exported ie: export DB_PORT=3306 - I start my program including the env variables like: source ./env.sh; ./some-program
|
|
May 1, 2014 at 20:18 | comment | added | Mecki |
So simple, yet so elegant. And I like your answer better than the accepted answer, as it will start a sub shell equal to my current one (which may not be bash but could be something else, e.g. dash ) and I don't run into any trouble if I must use quotes within the command args (someargs ).
|
|
Jan 8, 2014 at 1:04 | comment | added | 0xC0000022L |
@PopePoopinpants: why not use source (aka . ) in that case? Also, the backticks shouldn't be used anymore these days and this is one of the reasons why, using $(command) is waaaaay safer.
|
|
Jan 7, 2014 at 21:23 | comment | added | Craig Monson |
If you have a ton of environment variables and want to load them via a file, you can do that similarly to the above by adding all your export FOO=bar settings into a file, then running the above like so: (`cat heroku_prod_config` && command some args)
|
|
Apr 16, 2013 at 19:45 | comment | added | rampion |
In zsh I don't seem to need the export for this version: (FOO=XXX ; echo FOO=$FOO) ; echo FOO=$FOO yields FOO=XXX\nFOO=\n .
|
|
Jun 1, 2012 at 20:37 | comment | added | Keith Thompson |
@MartyMacGyver: && executes the left command, then executes the right command only if the left command succeeded. ; executes both commands unconditionally. The Windows batch (cmd.exe ) equivalent of ; is & .
|
|
Jun 1, 2012 at 20:09 | history | edited | 0xC0000022L | CC BY-SA 3.0 |
added 135 characters in body
|
Jun 1, 2012 at 19:33 | comment | added | MartyMacGyver | This appears to work, though it's not quite what I was looking for (it may be that there is no way to do what I'm describing above in one command without explicitly using export first...) Thoughts? | |
Jun 1, 2012 at 19:25 | history | answered | 0xC0000022L | CC BY-SA 3.0 |