I have this command

$("${DIR}/test.py" "$1")

the output of which is

export SOME_VAR="hello world"
export ANOTHER_VAR="hello"

But eval $("${DIR}/test.py" "$1")

seems to do nothing. What am I doing wrong?

I want to export those variables in my shell script.

Full bash script:

DIR="$(cd `dirname $0` && pwd)" 
eval $("${DIR}/test.py" "$2") 
exit $? 

Python script (not full script but give a good idea):

print('export var1="{0}"\nexport var2="{1}"\nexport var3="{2}"\nexport var4="{2}"'.format(string1, string2, string3))
  • 1
    It works for me... bash-4.2$ ./x export SOME_VAR="hello world" export ANOTHER_VAR="hello" bash-4.2$ eval $(./x) bash-4.2$ echo $SOME_VAR hello world . Note you shouldn't have the second export in the output, but it won't stop the code working – Stephen Harris Nov 18 '18 at 18:39
  • In total, i need to export multiple variables. For me it doesn't stop the code from working but it also doesn't export any vars to the environment. I tried using both #!/bin/bash and #!/bin/sh i am on bash3.2 – masterpiece Nov 18 '18 at 18:47
  • 2
    Are you trying to export the variables to the current shell running the script, not just for the script environment? – kevlinux Nov 18 '18 at 18:55
  • @kevlinux yes, trying to export to current shell so other programs can use these vars. – masterpiece Nov 18 '18 at 18:56
  • 1
    Welcome to Unix & Linux. Are you trying to export a variable to a parent process from a child process? – Fabby Nov 18 '18 at 20:14

The example given works for me as well, but there is one potential source of trouble: depending on the values being defined, you may need double-quotes around the command substitution to prevent word splitting and wildcard expansion:

eval "$("${DIR}/test.py" "$1")"

Note that at the very least, not having double-quotes will cause troubles if the output contains multiple commands separated by newlines. That is because without the double-quotes, the output gets split into "words" based on whitespace (generally spaces, tabs, and newlines), and then eval splices it back together with spaces. Net result: all newlines get converted to spaces, and so all the commands that're supposed to be separate lines get mushed together into a single long mess of a command.

For an extreme example, suppose test.py outputs this:

# Define some variables:
export SOME_VAR="6 * 5"

The split-expand-merge process gives this resulting command:

# Define some variables: export SOME_VAR="6 file1.txt file2.txt otherfile.png 5"

(where "file1.txt file2.txt otherfile.png" are all the files in the current directory.) This isn't what you want at all.

  • thx Gordon. I will keep your tip in mind. The eval "$("${DIR}/test.py" "$1")" didnt solve it for me though. – masterpiece Nov 18 '18 at 19:55
  • posted full script – masterpiece Nov 18 '18 at 20:17

The problem was not using source to allow exporting vars to the current shell.

Not the answer you're looking for? Browse other questions tagged or ask your own question.