17

I wonder if there is a need to use "export" when setting a variable in .bashrc.

In my tests editing .bashrc there was no difference between

foo=bar

and

export foo=bar

In both cases, after login "echo $foo" outputs "bar".

I am using Debian Squeeze, if that matters.

Thank you guys in advance.

0

3 Answers 3

14

Try creating a shell script that accesses the foo variable.

If foo was export'ed, it will be visible in the shell script, otherwise it won't.

Sign up to request clarification or add additional context in comments.

4 Comments

I've created a test script containing "echo $foo". In both cases running the script after login the output was "bar". Am i crazy? Can anyone reproduce this?
How do you run the script? ./script or . script?
I run the script via ./script
Sorry guys. I don't know what went wrong. But now all works as expected. Thank you very much for all your replys!
13

SuperUser has this covered.

Short answer: export makes sure the environment variable is set in child processes. If you don't export, it's only available in the same process/interactive session.

1 Comment

I've created a test script containing "echo $foo". In both cases running the script after login the output was "bar". Am i crazy? Can anyone reproduce this?
5

It's preferable because exported variables get passed to child processes (programs launched from that shell). Without the export command those variables only apply to the shell itself and not processes launched from the shell

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.