I'm trying to use env to set environment variables (read from another source, say for example a file) for a subprocess. Essentially, I am attempting the following:env VALUE=thisisatest ./somescript.shIf, for example, somescript.sh was:echo $VALUEThen this would print thisisatest as expected. But I would like to load the variables from a file. I've gotten to this point:env $(cat .vars | xargs -d '\n') ./somescript.shBut, I run into trouble when any of the variables contain spaces (and quotes don't work as expected). So for example:env $(echo 'VALUE=this is a test' | xargs -d '\n') ./somescript.shWill error withenv: is: No such file or directoryAnd trying:env $(echo 'VALUE=thisisatest' | xargs -d '\n') ./somescript.shWill give me the unexpected:thisisatestI assumed this would work properly since running env VALUE=thisisatest ./somescript.sh prints it without the quotes.From the error, I glean that for some reason env is not understanding that the quotes mean that the value that follows should be a string. However, I'm unsure how to interpolate these vars in a way that the quotes are correctly interpreted.Can anyone provide any hints for how I could accomplish this?Thanks!
Setting environment vars containing space with env
bash;environment variables;env
You need double quote in command substitution, otherwise, the shell will perform field splitting with the result of command substitution:$ env $(echo 'VALUE=this is a test') ./somescript.shthis is a testFor env reading from file, you must let the shell does field spliting, but set IFS to newline only, so your command won't break with space:$ IFS=''$ env $(cat .vars) ./somescript.shIf you want to read from file, it's better if you just source (aka dot) the file in somescript.sh:#!/bin/bash. .vars: The rest of script go here