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
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:
Use printenv command to view the newly created environment variable:
One important environment variable is $PATH:
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.