Setting environment variables for multiple commands in bash one-liner

Assuming you actually need it as an environment variable (even though the example code does not really need an environment variable; some shell variables are not environment variables):

(export MYENVVAR=myfolder; echo $MYENVVAR && ls $MYENVVAR)

If you don’t need it as an environment variable, then:

(MYENVVAR=myfolder; echo $MYENVVAR && ls $MYENVVAR)

The parentheses create a sub-shell; environment variables (and plain variables) set in the sub-shell do not affect the parent shell. In both commands shown, the variable is set once and then used twice, once by each of the two commands.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)