FUNDAMENTALS A Complete Guide for Beginners
The Environment in Linux is an essential concept in the Linux system. You’ll find information about many different applications and programs in the Environment. You can also see the behavior and resources a program uses with Environment variables in Linux. In this article, I will discuss all the features and aspects of the environment in Linux.
What is the Environment in Linux?
The Environment in Linux contains much information using different variables. The Linux system stores those Environment variables within the system and has keys or names and values for that key. Changing Linux Environment variables, you can change many things in Linux systems, like changing appearance, creating shortcuts or paths for executable applications, default Home directories, and many more.
Variables in Linux
Variables are key-value pairs that contain different information for the system. There are 2 different types of variables.
A. Environment Variables
Environment variables are variables that are set to the whole system. The system inherits these variables along with all the shells in the system.
You can view the list of Environment variables in the command line using different commands.
- env (This command can run another command in a custom environment without modifying the current environment. But if used without an argument, all the variables are printed in the command line)
- printenv (Prints out all the environment variables of the system)
B. Shell Variables
Shell variables are the variables that are present only in the current shell. When the shell is closed, the shell variables are also erased from the system.
Variable Formats
The variables in Linux are by default written in Uppercase letters. They are case-sensitive, but you can use them in lowercase letters. The general format to declare a variable is the following.
KEY = "Values of the Key"
Note: You can use a single quotation mark or a double quotation mark. If there is no space between the words, no quotation mark is required.
If there are multiple values for the same key, you can use the colon (:) symbol between the values.
KEY = Value1:Value2:Value3
Creating an Environment Variable
Even among the Environment variables, there are differences. Some variables are present only until the shell session. But there are also some variables that don’t get erased. There are 2 types of Environment Variables.
1. Temporary Environment Variables
Temporary variables remain only until the shell remains. Most of the time, these variables are used and when the work session is done, the variable is erased along with the shell. Creating a temporary Environment variable is very easy. Just use the following syntax.
export MY_VAR="Value of the Variable"
But, you can’t view the content of the variable by just typing the variable. if you want to view the inside of the variable, you need to use the echo command with a dollar ($) sign.
echo $MY_VAR
Output >
You can check it out using the following command.
Now type the following command to see the variable:
printenv MY_VAR
Output >
In the command line, you will see the variable.
2. Permanent Environment Variable
Permanent variables don’t get erased automatically. That means, at the time you create the permanent variable, until you delete it, it will remain. But to create a permanent variable is a bit tricky. You need to access the /etc/environment file and ~/.bashrc file. You need to follow the steps below to try it out.
Steps to Follow >
❶ Open the Command Line in the Home directory.
❷ Type the following command to open the /etc/environment file.
sudo nano /etc/environment
❸ Write the following line inside the file.
MY_VAR="Value of the Variable"
❹ Save the file by pressing CTRL+S and Exit by pressing CTRL+X.
❺ Now open the ~/.bashrc file using the command below.
nano ~/.bashrc
❻ At the end of the file type the following line.
export MY_VAR="Value of the Variable"
❼ Save the file by pressing CTRL+S and Exit by pressing CTRL+X.
❽ Type the command to update the changes.
source ~/.bashrc
❾ Now you can check whether the variable is updated or not using the following commands.
echo $MY_VAR
printenv MY_VAR
Output >
Writing inside the /etc/environment file.
Writing inside the ~/.bashrc file.
Checking the Environment variable.
System Environment Variables
There are many system Environment variables present in Linux systems. Here you’ll find some of the most used Environment Variables in Linux.
- USER (The current user)
- HOME (The Home directory of the current user)
- SHELL (The current user’s shell. By default it’s bash)
- PATH (The Directories that need to be searched before executing the command)
- LANG (The current Language and Locale settings)
- TERM (Shows the type of Terminal to emulate)
- MAIL (Location of the current emails)
- EDITOR (Default file editor)
- LOGNAME (The current username)
Conclusion
In this article, you’ve seen different variables, variable formats, creating different types of variables, and many system variables. These are advanced uses of Linux that you will need to know to become a professional. But in the backend, many system variables are made and used without your knowledge. So, learning them will let you understand the basics of the Linux system.
Similar Readings