How to set and list environment variables in Linux
In the past, to use Linux, you needed to get acquainted with the command line immediately. Desktop environments like GNOME and KDE have made using Linux much easier for beginners. You can use Linux for a long time without knowing any terminal command.
However, if you want to do something that requires using the command line, you will still need to know about environment variables. This may seem like a complicated term, but actually environment variables are easy to understand.
What is an environment variable?
Environment variables allow you to set options on different programs without having to edit the application's configuration file. This applies across the system, so they are available not only in programs but also in shells and even subprocesses.
They are used to help different commands know where the Home folder is, what username and shell you are currently using. You can also set different options like terminal color using environment variables.
List environment variables
In fact, you can spend more time setting different environment variables than listing them. If you troubleshoot a problem, it's helpful to know how certain variables are set or whether they are set.
This aspect has two parts. You can list all current environment variables or choose to consider only a single variable value. To list all environment variables currently set, run the printenv command with no arguments:
printenv
This command will give you a high level overview of various environment variables. You can use this command to see what has been set or use it with another tool like grep to search for certain text strings.
If you only want to see the value of a variable, run printenv with the variable name you choose as an argument:
printenv HOME
If instead you want to test a few different variables, pass them all as arguments as the following example:
printenv HOME PWD
To pass the value of an environment variable, reference it with the $ character like the following example:
ls $HOME
Set environment variables
Setting an environment variable is also quite easy. Use the name without the $ operator and assign it with the = operator. For example, to set 'EXAMPLE_VAR' to 'hello', you would run the following command:
EXAMPLE_VAR=hello
You can now access this variable as above using the $ operator.
echo $EXAMPLE_VAR
This will only set the variable for the current session. When you log out or restart the computer, this variable will disappear. This is good for testing or if you just need to temporarily set a certain value.
To ensure these environment variables exist, you need to put them in the appropriate configuration file. You can use the '/ etc / environment' file for system-wide variables, while '/ etc / profile' sets the shell variables.
For personal use, you can put variables in '~ / .bashrc' or a similar file, if you use another shell. To set them up, use the same format as above.
With the command line tools above, you will have the basics for almost everything to do with environment variables. You can start by customizing shell parts or other boot options, then expand from there.
Of course, if you're new to Linux, you need to know more than just environment variables to make the most of the command line. Check out the article: Basic Linux commands everyone needs to know in order to learn more details.
Good luck!
You should read it
- 5 reasons why Linux distros create their own desktop environment
- Learn about Linux MATE desktop environment
- How to add an environment variable in Windows 10
- Variable type in C / C ++
- Instructions for installing Unix / Linux
- 5 ways to make Linux desktops look great
- 6 best Linux desktop environments in 2022
- Variable in C programming
May be interested
- Is Linux really immune to viruses and malware?when you switch to linux, you think you no longer have to worry about viruses and other types of malware. but even though this is almost always true, linux desktops are not completely secure.
- 6 reasons why Linux phones and laptops are not cheapif you want to buy a pc with pre-installed free and open source desktop, then you have to pay some money, and the amount you can pay can be quite large.
- How to check your Linux versionif you need to find the actual linux version or even the linux kernel version, please read the following article.
- How to reduce video file size with FFMPEG from Linux Terminalreduce the file size to help store larger files, and continue to increase the number of files in the collection easier. some methods of doing this are more effective than others.
- 7 great Linux distro media centers for HTPCif you are looking for an outdated linux media center or an alternative to kodibuntu, try the following open source htpc operating systems.
- How to scan malware and rootkits on Linux serverthere are many tools to help scan linux servers for malware and rootkits. this article will provide some of the best options to help deal with these cyber-enemies.