31

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?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
JohnJ
  • 6,736
  • 13
  • 49
  • 82
  • 8
    export isn't a real command (i.e. not in /bin, /usr/bin etc.) it's handled by bash internally. You could do bash -c "export foo=bar;echo \$foo" if you wanted to... – Adam Apr 11 '12 at 15:23
  • What is this custom software you speak of? – Captain Giraffe Apr 11 '12 at 15:23
  • 1
    What Adam says might be the key to your problem. `export` is a bash builtin. `echo` is a binary that resides in your `$PATH` – ArjunShankar Apr 11 '12 at 15:24
  • What is the 'custom (given by client)' software? – ArjunShankar Apr 11 '12 at 15:25
  • Thanks you guys for your response. The "custom software" is a bunch of java code my client has written, and it contains a feature to run linux commands (basically passes the commands as a string to shell). I will try the bash -c option and update you guys with the result. – JohnJ Apr 11 '12 at 15:28
  • Adam, amazing! bash -c does the trick. I am not entirely sure what bash -c does (I am on my way to google this), but any insight from you would help too! Could you please answer it as an "answer" and I will accept this? – JohnJ Apr 11 '12 at 15:31
  • Adam, on the same note, once I set the env variable using bash -c "export foo=bar", can I do something like: bash -c "cp im.log bm.log image1.png \$foo"? When I try doing this, I get a foo: undefined variable error :( – JohnJ Apr 11 '12 at 15:42
  • You should do it the way Adam mentions. `bash -c "export foo=bar; cp im.log bm.log image1.png \$foo"`. i.e. Chain the commands separated by `;`. Each invocation of `bash -c` starts with a fresh environment. Obviously, the variable is reported as undefined. – ArjunShankar Apr 11 '12 at 17:54
  • Arjun, thanks for that. If you could write it as an answer Id be glad to accept it. – JohnJ Apr 12 '12 at 23:25
  • @JohnJ - Done. Feel free to answer your own question as well! It is good to have questions 'answered' irrespective of who does it. – ArjunShankar Apr 13 '12 at 16:49

8 Answers8

39

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"
ArjunShankar
  • 23,020
  • 5
  • 61
  • 83
  • 1
    The "ALSO" paragraph helped me solve my problem. Thanks – Andreas Panagiotidis Oct 19 '16 at 18:24
  • 1
    I executed `$ bash -c "export foo=bar"`, but it says `$ bash -c "echo \$foo" foo: Undefined variable.` –  Jun 06 '17 at 19:06
  • This does not overwrite the value on my centos 5.11, the echo in this line still prints the old value – DavidDunham Sep 20 '18 at 08:14
  • @DavidDunham there are two snippets in the answer: The first one calls export and echo in *one* bash invocation and the other in two separate ones. Accordingly, the first one should "overwrite" the variable (although only temporarily, and then print the new value) and the second should not. What did you observe? – ArjunShankar Sep 20 '18 at 09:11
5

If you are using C shell -

setenv PATH $PATH":/home/tmp"
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
karuna
  • 59
  • 1
  • 2
5

If you can’t use the "export" command then just use:

setenv path /dir

Like this

setenv ORACLE_HOME /data/u01/apps/oracle/11.2.0.3.0
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
1

Probably because it's trying to execute "export" as an external command, and it's a shell internal.

theglauber
  • 28,367
  • 7
  • 29
  • 47
1

Changing from Bash to sh scripting made my script work:

!/bin/sh
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Jackie
  • 25,199
  • 6
  • 33
  • 24
  • actually you were right as well. sometimes the default shell is not either bash or sh, using this command fixes the issue – Amir Nov 05 '14 at 20:13
0

Are you certain that the software (and not yourself, since your test actually only shows the shell used as default for your user) uses /bin/bash?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Vincent L.
  • 122
  • 3
0

SHELL is an environment variable, and so it's not the most reliable for what you're trying to figure out. If your tool is using a shell which doesn't set it, it will retain its old value.

Use ps to figure out what's really going on.

bukzor
  • 37,539
  • 11
  • 77
  • 111
-3

Follow these steps to remove "bash export command not found." Terminal open error fix>>>>>>

Open a terminal and type:

nano ~/.bashrc

After loading nano:

Remove the all 'export PATH = ...' lines and press Ctrl + O to save file and press Ctrl + E to exit.

Now the terminal opening error will be fixed...

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Hasee Amarathunga
  • 1,901
  • 12
  • 17