python - os.getenv and os.environ don't see environment variables of my bash shell

后端 未结 2 490
萌比男神i
萌比男神i 2020-12-24 06:45

I am on ubuntu 13.04, bash, python2.7.4

The interpreter doesn\'t see variables I set.

Here is an example:

$ echo $A
5
$ pyth         


        
相关标签:
2条回答
  • 2020-12-24 07:21

    Aha! the solution is simple!

    I was setting variables with plain $ A=5 command; when you use $ export B="kkk" everything is fine.

    That is because export makes the variable available to sub-processes:

    • it creates a variable in the shell
    • and exports it into the environment of the shell
    • the list environment is passed to sub-processes of the shell.

    Plain $ A="kkk" just creates variables in the shell and doesn't do anything with the environment.

    The interpreter called from the shell obtains it's environment from the parent -- the shell. So really the variable should be exported into the environment before.

    0 讨论(0)
  • Those variables (parameters in bash terminology) are not environment variables. You want to export them into the environment, using export or declare -x. See the bash documentation on environment.

    0 讨论(0)
提交回复
热议问题