nonopolarity
nonopolarity

Reputation: 150976

In Bash, why can we set some environment variable by PS1='something' and others need to be export SOME_VAR='something'?

And why is an export needed? Where is it exporting to?

Upvotes: 7

Views: 563

Answers (2)

mvds
mvds

Reputation: 47034

Exported variables are passed on to new processes invoked.

Try setting A=1, then invoking a new shell by entering "bash", then echo $A - an empty line.

Do the same, but then export A=1, invoke a new shell, then echo $A - voila!

edit on the technical side, and looking at your question, B=1 doesn't actually set an environment variable. To get the real environment of your shell (in linux), try

$ xargs -n 1 -0 echo < /proc/$$/environ

which differs from the output of export. And as a sidenote, this question touches on the internals of bash and its environment handling.

Upvotes: 7

Steve Emmerson
Steve Emmerson

Reputation: 7832

The PS1 environment variable is pre-defined by the bash shell; consequently, it doesn't need to be exported, merely set.

Upvotes: 1

Related Questions