Shell Environment Variables
An environment variable is a variable the shell shares with the programs it runs. HOME and PATH are two well-known environment variables.
Share a variable
export marks FAVOURITE for sharing, and env lists it.
export FAVOURITE=tea
env | grep FAVOURITEExport a variable set earlier
A variable set first can be exported in a second step.
FAVOURITE=tea
export FAVOURITE
env | grep FAVOURITEPrint the home folder
HOME holds the home folder, so echo $HOME prints its path.
echo $HOMEExample
export FAVOURITE=tea
export CITY=Leeds
env | grep FAVOURITE
env | grep CITYKey points
- Use export name=value so programs the shell runs can use the variable.
- Use env to list the environment variables.
- HOME: the environment variable that holds the home folder.
- A plain variable stays in the shell, and only export shares it.
In this example, export shares FAVOURITE and CITY with programs the shell runs. Then env lists each one, and grep picks it out.
Try it Yourself
Edit the code, then press Run.
Running is simulated
Exercise
What does export do?