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.
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
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"
set | grep VAR1
VAR1="my variable 1"
printenv | grep VAR1
echo $VAR1
my variable 1
exit
export VAR1="my variable 1"
export VAR1
set | grep VAR1
printenv | grep VAR1
VAR1="my variable 1"
echo $VAR1
my variable 1
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"
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
$ printenv | grep MYVAR
MYVAR=test
$ export -n MYVAR
$ printenv | grep MYVAR
$ echo $MYVAR
test
$ unset MYVAR
$ echo $MYVAR
Internal link
May be of interest to you:
No comments:
Post a Comment