What are environment variables in Linux?

 


Environment variables are set of dynamic named values, stored within the system that are used by the apps launched in shells. In this article, you can read more on environment variables, how to list them and how to read them. 


In simple word, an environment variable is a variable with name and associated value. Each environment variable are written with capital letters, such as HOME, BASH and etc. 


We can list all the variables within shell by typing the following command:


# env 




On the image above are listed all the variables within shell. Besides the shell, there are global variables as well. 


Some of the most common used global variable are as follow HOME, PWD, SHELL, USER, TERM, PATH and etc. 


The HOME variable will display the user home directory. TO access it, we simply type in the following command.


# echo $HOME




The SHELL variable will list the name of the shell launched when you open up the terminal window. It will be bash because it is currently common used shell in most Linux distribution. To view the shell variable, simply type in the following command.


# echo $SHELL 




The USER variable will display the current logged user on a Linux machine. To view it, type in the following.


# echo $USER




The most important variable in Linux is the PATH. This variable list directories that the shell will search through to find command executables. To display it, type in the following command.


# echo $PATH




When you run a command, it is the PATH environment variable which is being used to determine where to look for a command. One common location is the /bin directory. It's a directory which contain some of the most commonly used commands such as pwd, ls, cp, mkdir, touch, dd, df, du, tail, less, more, and etc. 


Therefore the PATH variable is very useful because it allows the command to be executed without knowing or specifying the full path to the command. 


Each directory within the PATH variable is separated by a (:) semicolon sign. If you wish to add your /home directory into a PATH, you must type in /home/amar: and the way you can do that is being shown in the following command.


# PATH = /home/amar/:$PATH

# export PATH=/home/amar/:$PATH




As you can see, my /home directory is now available inside the PATH variable. Other words, now I can execute any application from within the /home directory simply by typing the following command ./name_of_file.


In the following sample, I have created a very simple bash script that will print out the present working directory followed by a quick note: 'Your present working directory is?'. Check out the sample script. 




Once you type in your script, you must run the following command to make the script executable.


# chmod +x  myfile.sh 


Once you do that, you can run the script by typing the following command.


# ./myfile.sh




As you can see, I have run this script within my /home directory which I have added into PATH variable. This way, I don't have to type in the full path to the .myfile.sh script in order to run it, I have simply type in ./myfile.sh and that's it. 


That was on environment variables for this time. Please make sure to read other online resources and websites on the environment variables that are listed bellow. Till next time. 


[1] Environment variables in Linux and how to set them?

https://www.howtogeek.com/668503/how-to-set-environment-variables-in-bash-on-linux/

Share:

Nema komentara:

Objavi komentar

Zajednica

POPULARNI POSTOVI

Oznake

NAJNOVIJE