Set environment variable for subshell
One way:
FOO=bar sh -c 'first && second'
This sets the FOO
environment variable for the single sh
command.
To set multiple environment variables:
FOO=bar BAZ=quux sh -c 'first && second'
Another way to do this is to create the variable and export
it inside a subshell. Doing the export
inside the subshell ensures that the outer shell does not get the variable in its environment:
( export FOO=bar; first && second )
Summarizing the (now deleted) comments: The export
is needed to create an environment variable (as opposed to a shell variable). The thing with environment variables is that they get inherited by child processes. If first
and second
are external utilities (or scripts) that look at their environment, they would not see the FOO
variable without the export
.