Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bash export command

Tags:

linux

ubuntu

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?

like image 643
JohnJ Avatar asked Apr 11 '12 15:04

JohnJ


People also ask

What does the export command do?

The export command is fairly simple to use as it has straightforward syntax with only three available command options. In general, the export command marks an environment variable to be exported with any newly forked child processes and thus it allows a child process to inherit all marked variables.

How do I export a file in Linux?

Export is defined in POSIX as The shell shall give the export attribute to the variables corresponding to the specified names, which shall cause them to be in the environment of subsequently executed commands. If the name of a variable is followed by = word, then the value of that variable shall be set to the word.

What is export in CMD?

The export combined with the import command allows you to batch install applications on your PC. The export command is often used to create a file that you can share with other developers, or for use when restoring your build environment.

What is export profile in Bash?

When you use export , you are adding the variable to the environment variables list of the shell in which the export command was called and all the environment variables of a shell are passed to the child processes, thats why you can use it.


2 Answers

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" 
like image 74
ArjunShankar Avatar answered Oct 07 '22 17:10

ArjunShankar


If you are using C shell -

setenv PATH $PATH":/home/tmp" 
like image 40
karuna Avatar answered Oct 07 '22 17:10

karuna