Monday, 10 July 2017

Environment variables in Linux

Introduction to Linux

Linux is an Operative System (OS) and I think it's the best OS in software development. The initial versions were known as Unix and it was originally developed for personal computers based on the Intel x86 architecture. Nowadays distributions are available for each type of device (Server, PC, Smartphone, Tablet, Video game consoles etc...). 
After this brief introduction to Linux we can begin to introduce the environement variables.

Variables overview in Linux

In general a variable is used to store information to be referenced and manipulated in a software program. In linux, like in programming languages, variables are used with almost all basic features as the scope.
The basic syntax for defining a variable involves initialization but not the definition of the type. In particular, every time a shell session spawns, it is created a process that gathers and compiles information that should be available to the shell process and its child processes. A variable is represented as key-value pair. 
 KEY=value
If a variable has a multiple values, the values are separated by colon (:) characters. 
 KEY=value1:value2:valueN
If the value contains significant white-space, quotations are used as showed below:
 KEY="value 1"
About the scope, the variables can classified in two categories: "Environment variables" and "Shell variables". The environment variables are variables that  are defined for the current shell and are inherited by any child shells or processes. Instead the shell variables are variables that are contained exclusively within the shell in which they were set or defined. You can read the full list of environment variables using the bash command "printenv" without parameters as shown below:
$ printenv
Instead, if you want the value of the variable specification, add it as parameter to the previous command:
$ printenv <name_variable>
Example:
$ printenv USER
 user01

Creating Shell Variables and Environmental Variables

Premised that a Shell Variable is different from an Environmental Variable, the following paragraph will show how to create them, so you can understand the differences in practice. A Shell Variable can be create via bash:
VAR1="my variable 1"
Now you can see the shell variable with the following command:
set | grep VAR1
VAR1="my variable 1"
Instead if you use the following command:
printenv | grep VAR1
The output will be blank. Finally, using command below:
echo $VAR1
the output will be:
my variable 1
you can get back to our original shell by typing with:
exit
Now we create an Environmental Variable as below:
export VAR1="my variable 1"
If you not execute the exit command from same shell you can use only:
export VAR1
The result will be the same. If we execute the commands used previously for shell variable:
set | grep VAR1
or
printenv | grep VAR1
the output will be:
VAR1="my variable 1"
Finally with behavior of echo command will be as case of shell variable:
echo $VAR1
my variable 1
If you execute before exit command and retry the echo command the output will be blank.

Setting the Environment Variables at login

One distinction between different sessions is whether the shell is being spawned as a "login" or "non-login" session. A "login" shell is a shell session that begins by authenticating the user. Instead when you start a new shell session from within your authenticated session, a "non-login" shell session is started. 
Whether a shell session is classified as a login or non-login shell has implications on which files are read to initialize the shell session. A session started as a "login" session will read configuration details from the /etc/profile file first. Instead a session defined as a "non-login" shell will read /etc/bash.bashrc and then the user-specific ~/.bashrc file to build its environment.
Most Linux distributions configure the login configuration files to source the non-login configuration files. This means that you can define environmental variables that you want in both inside the non-login configuration files. This means that the place to define these variables is in the ~/.bashrc file.
Open it with an editor as vi, vim, nano, gedit etc..., so, for example, add a new environment variable as shown below:
vi ~/.bashrc
Now append the following line to add an environment variables with VAR1 as name and "my variable 1" as value:
export VAR1="my variable 1"
The environment variables will declared in next time when you start a new session.

Demoting and Unsetting Variables

We make a full turn of the life cycle of the variable until her delete. You create a new environment variable as shown in the preceding paragraphs:
$ export MYVAR=test
Now you could use the printenv command to read MYVAR value:
$ printenv | grep MYVAR
MYVAR=test
Now you could try the demoting of an environment variable:
$ export -n MYVAR
if you retry the printenv command then you will notice that the variable is no longer in the list.
$ printenv | grep MYVAR
Instead the result will be different if you use the "echo" command as shown below:
$ echo $MYVAR
test
To remove the variable and her vale then you could use the "unset" command as shown below:
$ unset MYVAR
Now if you retry the "echo" command then you will notice that the result will be "blank":
$ echo $MYVAR 
Now the output is blank and MYVAR are not present.

Internal link

May be of interest to you:

No comments:

Post a Comment

Welcome

Hello everybody, Welcome in my blog called "Information technology archive". Obviously the topics will be related to Informatio...