Each time we launch a terminal window in the shell inside it, a collection of predefined variables 1
are set with some dynamic values and are used to customize the way the system works and behaves. 2
There are two classes of such variables: environment, variables and shell variables. 3
Environment variables are defined for the current shell and are inherited by any child shells or processes. 4
They are used to pass information to processes that are spawned from the current shell. 5
On the other hand, the shell variables are contained exclusively within the shell in which they are 6
defined or set. 7
Just to give you an example to make my point, PATH is such an environment variable that holds a 8
list of directories separated by columns where the shell will search for commands your run. Let's see 9
the value it holds. 10
When assigning multiple values to such a variable, it's common to separate them by a colen. Environment 11
variables are written by convention in uppercase letters. 12
Most of the time you don't use environmental variables directly. 13
They are referenced by individual applications and services as needed. 14
For instance, your home directory is set as an environment variable called home 15
when you log in. The system knows your home directory by reading the value of this variable. 16
Other environment variables will store information about the default browser or text editor, the keyboard 17
the layout settings and many more. 18
You can view all environmental variables set on your system, running the env command. The list is 19
so long, so pipe the output through less to make it easier to read: env | less 20
These are all environment variables. 21
And to search for a specific variable pipe the output to grep. 22
Another useful commmand that will list the environment variables, but also the value of a specific 23
variable is print env. 24
Running it without any argument will print out all environmental variables into giving a variable. 25
as argument will print out the value of that variable. 26
For example, printenv home and it's printing the value of the home variable. 27
You can also pass more than one argument to the print and env command like this: printenv SHELL 28
PWD 29
LC_TIME , local time, and o on 30
env and the printenv will print out all environment variables. To display all available variables, 31
environment, shell and those created by the user use the set command. 32
For example, I'm creating a new variable, abs=123 and I'm searching for it 33
in the output of the env command. 34
So env | grep abc the name of the variable. 35
And it's not there. Now, I'm searching for it in the output of set. 36
And I found the variable. 37
The set command is displaying shell functions as well and the output of information is overwhelming 38
To clean up the output, we specify that set should operate in POSIX mode so it won’t print the shell . 39
functions set -o posix;set and by running the set command 40
And now it's displaying only the environment and shell variables. 41
Environment variables can be used when you want to change the default settings of your system, for 42
example, you want to add a directory to the path so that you can run the scripts in that directory 43
by typing their name. Let's see how to do it! 44
I’ll add the scripts directory from the user’s home directory to PATH. 45
To do that, I have to edit a startup configuration file. 46
There are many such files and it can also depend on the distribution you are using, but in general 47
is .bashrc for the Bash shell in the user's home directory. 48
I'm opening it and at the end I'm writing. 49
Export PATH=$PATH the current value : and the new directory in the user's home 50
directory ~/scripts 51
I'm saving the file and loading it in the current shell source .bash 52
Now, the PATH variable contains the scripts directory. 53
This is the directory and any script or command found in that directory can be run by name as any other 54
command without writing ./ before its name. 55
For example, while 1.sh and it is working; it found the command otherwise it would have returned command 56
not found like this. 57
To create a new environment variable, you have to export it using the export command before the variable 58
name. 59
So in a startup configuration file like .bashrc I’m adding 60
export MYVAR 61
= and a random ”value” 62
This is a user specific environment variable, and if you want to create a system wide variable available 63
for all users, declare the variable in its /etc/ profile or /etc/bash.bashrc. this file /profile 64
or /etc/bash.bashrc. 65
Of course, only root can edit those files. 66
There is also another file in the etc/ directory called environment. 67
It specifies the system wide environment variables to be set; the file must consist of a simple NAME 68
=VALUE pairs on separate lines.