How to Set Environment Variables in Linux

Environment variables are values that can affect the way running processes will behave on a computer. In Linux, you can set environment variables in several ways, including in the shell profile or startup script, or in the shell itself.

Here are the steps to set an environment variable in the shell:

  1. Open a terminal window
  2. Type the following command:
export VARNAME=value

Replace “VARNAME” with the name of the environment variable you want to set, and replace “value” with the desired value. For example, to set the “PATH” variable, you would use the following command:

export PATH=$PATH:/usr/local/bin

This adds the “/usr/local/bin” directory to the existing PATH variable.

  1. To make the environment variable permanent, you need to add the export command to your shell profile. The location of the shell profile depends on the shell you are using. For example, if you are using the bash shell, you would add the export command to the “.bashrc” file in your home directory.

Once you have set an environment variable, you can use it in your shell scripts or other programs. To access the value of an environment variable, use the following syntax:

$VARNAME

Replace “VARNAME” with the name of the environment variable you want to access. For example, to access the value of the “PATH” variable, you would use the following command:

echo $PATH

Leave a Comment