I have a variable AAA which is in this format
AAA='BBB=1 CCC=2 DDD=3'
How can I use this to set environment variables BBB, CCC and DDD in a command I run (without permanently exporting them to the shell)? That is, I want to use to use the above to do something identical to:
# this works correctly: node is run with AAA, BBB, and CCC in its environment
BBB=1 CCC=2 DDD=3 node index.js
...however, when I try:
# this does not work: AAA=1 is run as a command, so it causes "command not found"
$AAA node index.js
...it tries to run BBB=1 as a command, instead of parsing the assignment as a variable to set in node's environment.
eval $AAA node index.jsAAAas an array, and then pass it as normal command parameters, likeAAA=(BBB=1 CCC=2 DDD=3); node index.js "${AAA[@]}"?evalintroduces serious security and correctness issues; if your real command has more complex arguments it can easily corrupt them, and that's among the least harmful impacts. Considerenv "${array[@]}" node index.jsafter setting up an array as @BenjaminW proposes earlier.env, you can useenv -S "$AAA" node index.js.env $AAA node index.jssubjects$AAAto pathname expansion, so there's a slight risk of not getting what you expect. A value likeBBB=x -- ywill also not work as expected due to the shell's word-splitting.-Sletsenvhandle the splitting.