Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 626
nonopolarity Avatar asked Aug 05 '10 02:08

nonopolarity


People also ask

How do I set an environment variable in bash?

The easiest way to set environment variables in Bash is to use the “export” keyword followed by the variable name, an equal sign and the value to be assigned to the environment variable.

Why do we need to set environment variables in Linux?

Environment variables exist to enhance and to standardize your shell environment on Linux systems. There are standard environment variables that the system sets up for you, but you can also set up your own environment variables, or optionally change the default ones to meet your needs.

What are environment variables in bash?

Environment variables are special variables (like $HOME ) that contain information about your login session. They're stored for the system shell to use when executing commands. They exist whether you're using Linux, Mac, or Windows. Many of these variables are set by default during installation or user creation.


2 Answers

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.

like image 61
mvds Avatar answered Oct 07 '22 06:10

mvds


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

like image 45
Steve Emmerson Avatar answered Oct 07 '22 04:10

Steve Emmerson