bash export command

后端 未结 8 1327
迷失自我
迷失自我 2020-12-16 11:44

I am encountering a strange problem with my 64bit Ubuntu - on the export command.

Basically, I have got a VM installation on Ubuntu on my Win7, and I am trying to pa

8条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-16 12:19

    export is a Bash builtin, echo is an executable in your $PATH. So export is interpreted by Bash as is, without spawning a new process.

    You need to get Bash to interpret your command, which you can pass as a string with the -c option:

    bash -c "export foo=bar; echo \$foo"
    

    ALSO:

    Each invocation of bash -c starts with a fresh environment. So something like:

    bash -c "export foo=bar"
    bash -c "echo \$foo"
    

    will not work. The second invocation does not remember foo.

    Instead, you need to chain commands separated by ; in a single invocation of bash -c:

    bash -c "export foo=bar; echo \$foo"
    

提交回复
热议问题