Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
784 views
in Technique[技术] by (71.8m points)

linux - Bash export command

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

Basically, I have got a VM installation on Ubuntu on my Windows?7 system, and I am trying to pass commands from my Windows system to my VM installation using a custom (given by client) software.

So, on my VM, when I do:

export foo=bar
echo $foo

everything works as expected.

However, when I do the same through the custom software (which basically passes the Linux command as a string to the bash shell), I get:

export: command not found

I tried looking at the shell (using the custom software), using:

echo $SHELL > shell.txt

And I get /bin/bash which is expected and I still get the "export: command not found error".

How can I fix this?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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"

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...