Linux environment variables

Linux shell can be used to store temporary variable values that can be used to control the behavior of the shell or the state of bash scripts. A shell variable can be easily created by using the equal character (=). Below you will find an example in which a value is assigned to VAR variable:
You can view the current value of a shell variable by using the echo $variable_name command. Example: echo $VAR
Linux shell variable
Environment variables are used by the shell to pass their values to running programs that are executed by the shell. Different programs may use environment variables to set specific configurations and options. Check out the ENVIRONMENT section in command’s man pages to find out how to use these variables.
Environment variables are created just like shell variables, but you will need to use the export command afterwards:
export VAR
Use printenv command to view the newly created environment variable:
Linux environment variables

One important environment variable is $PATH:

PATH environment variable


This variable stores the location where batch commands are executed. Simply put, when executing a command within the shell, the PATH variable will be checked for the location of that particular command. On CentOS distributions, the PATH variable will be set by default to: /usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin. Note that the PATH variable stores folders in the search order.
You can add folders to the variable by executing the following command:
PATH=new_directory:$PATH – adds the new_directory to the beginning of the path
PATH=$PATH:new_directory – adds the new_directory to the end of the path
Note that if you accidentally empty the PATH variable, you will not be able to execute shell commands. To fix this issue simply close and restart the terminal.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s