Learn
Environment
Environment Variables

Environment variables are variables that can be used across commands and programs and hold information about the environment.

What happens when you store this in ~/.bash_profile?

export USER="Jane Doe"
  1. The line USER="Jane Doe" sets the environment variable USER to a name “Jane Doe”. Usually the USER variable is set to the name of the computer’s owner.
  2. The line export makes the variable to be available to all child sessions initiated from the session you are in. This is a way to make the variable persist across programs.
  3. At the command line, the command echo $USER returns the value of the variable. Note that $ is always used when returning a variable’s value. Here, the command echo $USER returns the name set for the variable.

Instructions

1.

Now that you are familiar with configuring greetings and aliases, let’s move on to setting environment variables.

Open ~/.bash_profile in nano.

2.

In the bash profile, beneath the aliases, on a new line, type:

export USER="Jane Doe"

Feel free to use your own name. Then, save the file and exit.

Then, once you’ve exited nano and are back in the terminal, press the Enter (or return for Mac) key onto a new line.

3.

In the command line, use source to activate the changes in the bash profile for the current session.

4.

Type:

echo $USER

This should output the value of the variable that you set.

Folder Icon

Sign up to start coding

Already have an account?