I have a command I execute in the bash which requires an environment variable. If I call it like this, everything works fine:
export MYVAR=value & my_first_command
But now I want to pipe the result to a second command, which requires the same environment variable. I tried this one:
export MYVAR=value & my_first_command | my_second_command
In that case, MYVAR seems not to be set for my_second_command. What's the correct syntax to make MYVAR available to my_second_command too?
export MYVAR=value && my_first_commandto executemy_first_commandif the previous command was successful.&, the variable ought to be exported in its do-nothing subshell only, and not tomy_first_command.export MYVAR=value ; my_first_command ; my_second_command?Scopeisn't quite the right concept here. That would imply thatMYVARisn't defined in the child, but that it could then somehow look up to the parent to see ifMYVARis defined there. Instead, the child is given its own copy ofMYVARwhen it begins, which is not affected even if the parent changes its value ofMYVARwhile the child is still running.