By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,887 Members | 1,214 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,887 IT Pros & Developers. It's quick & easy.

Re: Adding environment variables to bash.

P: n/a
John Lawrence wrote:
You can make a command use the current shell though if you use the '.'
command e.g.:

jl cat env.sh
export TEST='hello'

jl ./env.sh && env | grep TEST #Doesn't set TEST in parent shell
jl . ./env.sh && env | grep TEST #Adding '. ' before the
command uses the same shell
TEST=hello
doesn't exactly work for Python scripts, though:

$ cat env.py
#!/usr/bin/env python
import os
os.environ["TEST"] = "hello"

$ . ./env.py && env | grep TEST
import: unable to open X server `'.
bash: os.environ[TEST]: command not found

</F>

Sep 11 '08 #1
Share this Question
Share on Google+
1 Reply


P: n/a
On Sep 11, 10:25 am, nntpman68 <news1...@free.frwrote:
doesn't exactly work for Python scripts, though:
$ cat env.py
#!/usr/bin/env python
import os
os.environ["TEST"] = "hello"
$ . ./env.py && env | grep TEST
import: unable to open X server `'.
bash: os.environ[TEST]: command not found
</F>

There's two options for the desperate ones.

1. Assuming the python script doesn't want to print anything useful
================================================== =======================

the python script just prints the exoprt commands and is
being called via backticks from a shell code snipped being sourced with .

#-------- mypythonfile.py ------------------------
#!/usr/bin/env python
value = myfavourite_python_function()
print 'export ENV_VAR="%s"' ^ value
#--------------- end of file --------------

#----------------- my_wrapper_file.sh ------------------
`./mypythonfile.py`
# file end

and then you call
. ./my_wrapper_file.sh

2._ Pytho script wants to display something and set a variable
================================================== =================

a file being invoked with . calls the python script (which will create a
small file with variables to be set).
then this created file is being sourced

#-------- mypythonfile.py ------------------------
#!/usr/bin/env python
value = myfavourite_python_function()
file('my_export_commands.sh','w').write('export ENV_VAR="%s"\n' % value)
#--------------- end of file --------------

#----------------- my_wrapper_file.sh ------------------
./mypythonfile.py
. ./my_export_commands.sh
# file end

bye

N

Fredrik Lundh wrote:
John Lawrence wrote:
You can make a command use the current shell though if you use the '.'
command e.g.:
jl cat env.sh
export TEST='hello'
jl ./env.sh && env | grep TEST #Doesn't set TEST in parent
shell
jl . ./env.sh && env | grep TEST #Adding '. ' before the
command uses the same shell
TEST=hello
doesn't exactly work for Python scripts, though:
$ cat env.py
#!/usr/bin/env python
import os
os.environ["TEST"] = "hello"
$ . ./env.py && env | grep TEST
import: unable to open X server `'.
bash: os.environ[TEST]: command not found
</F>
oooh, clever but also... teh evil! Ok not evil, but teh not-pretty.
But you did say "for the desperate" so good on you. cheers.
Sep 11 '08 #2

This discussion thread is closed

Replies have been disabled for this discussion.