Linux 101 : Bash environment variables



Environment variables come in a "key=value" pair, they affect the behavior of programs and allow them to be more flexible. For example we can change the default editor of a program using an EDITOR environment variable as below:
  • EDITOR="Nano"
  • EDITOR="Vim"
  • - - - - - -

By keeping the environment variable separate from the program, we can easily change them without the need to change the code of the program and recompile it.

Displaying environment variables:

To see all the environment variables we use the below command:


Displaying a particular variable:

If we want to display a variable like the "PATH" environment variable, we use the below:


or:

Remark:

The Environment variables are case-sensitive, "Path" and "PATH" are completely different variables.

Creating a new environment variable:

We create a new environment variable using:


If the variable already exists, it will be overwritten by the new value.

Removing an environment variable:

We do that using the below command:


Persisting environment variable:

To be able to survive a reboot we will need to add our environment variable to the ".bash_profile" file in our home directory "~/.bash_profile" as below:


For that to take effect we would need to logout of the system and log back in.

Comments

Leave as a comment:

Archive