2

I want to run a series of bash commands generated by a Python script. The commands are of the form export foo="bar" and alias foo=bar. They must modify the environment of the current process.

This works great:

$(./generate_commands.py)

until an export command contains a space e.g. export x="a b". This generates an error, and only "a is exported (quotes included).

Currently I'm working around this by outputting generate_commands to a temporary file and sourcing that, but is there a more elegant solution?

4
  • 2
    Can you show what you're doing exactly? Commented May 2, 2013 at 15:28
  • Hopefully I made the question a bit clearer. Commented May 2, 2013 at 16:57
  • 1
    "They must modify the environment of the current process." That's not going to work. If you're currently running in python, then, regardless of the exact syntax or method of doing so, running bash in order to evaluate the commands you generated will start a separate process, which by definition cannot influence the environment variables of its parent process. Commented May 2, 2013 at 17:18
  • @twalberg no, it already does work, the question is whether it can work more elegantly. Commented May 2, 2013 at 17:25

2 Answers 2

5
./generate_commands | bash

This will pipe the output of the script as input to bash

Edit:

To allow for variables to be visible in the current shell, you need to source the output:

source <(./generate_commands)

or

. <(./generate_commands)
Sign up to request clarification or add additional context in comments.

5 Comments

Apologies for not making it clearer the first time around: the commands are mostly of the form export x=y and must modify the environment of the current process.
@MichaelPlatings then sourcing is your only way to go, everything else will only modify the subshell environment, answer edited
I guess that's the answer, thanks. Next problem: print 'export ' + key + '="' + value + '"' IOError: [Errno 32] Broken pipe
That looks like a python error. Have you executed this manually in a shell, outside of the script? Also try adding set -x or #!/bin/bash -x to view the debug statements made by bash script so that you can see what the lines being executed are, and which, if any, are failing.
Yes it's a python error. Executing it outside the script gives the same error. I guess python just doesn't play nicely with this method. In case it gives any insight, with bash -x I get the following output: ++ ./generate_commands.py + . /dev/fd/63 ++ echo -ne '\033]0;michael@comp: /my/working/dir\007'
0

I think the OP's problem is

cmd="export x=\"a b\""
${cmd}

does not work, but

export x="a b"

works. My way around this is

export x="a"
echo $x
x+=" b"
echo $x

3 Comments

Interesting idea, but cmd2="x+=\" b\"" ${cmd2} generates an error bash: x+=": command not found
@MichaelPlatings Did you put them in a script? it worked on my machine though :)
Yes. It works when typing the commands directly, but the error occurs running the commands from a script.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.