🎓 A1Learner
🔍
0 of 50 lessons completed

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 FAVOURITE

Export a variable set earlier

A variable set first can be exported in a second step.

FAVOURITE=tea
export FAVOURITE
env | grep FAVOURITE

Print the home folder

HOME holds the home folder, so echo $HOME prints its path.

echo $HOME

Example

export FAVOURITE=tea
export CITY=Leeds
env | grep FAVOURITE
env | grep CITY

Key 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?