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
- Use array in Shella shell variable is able to hold a single value. this type of variables is called scalar variables.
- 5 ways to make Linux desktops look greatvarious tools, tips and tweaks can be used to personalize linux desktops. use the following 5 methods to personalize your linux desktop environment.
- Learn about custom variables in Google Analyticscustom variables are name-value pair tags (name-values) that you can insert into your code to refine google analytics tracking.
- 6 best Linux desktop environments in 2022when it comes to things like desktop environments, it can be difficult to determine the best option for exactly what you'll be using.
- Differences in the types of desktop environments on Linuxwhen gnome 3.0 was released with the new interface, a part of the linux user community struggled to find a new desktop environment suitable for them.
- Git environment settingsbefore you can use git, you must install and make some configuration changes. here are the steps to install git client on ubuntu and centos linux.
- How to use Systemctl to list all Linux serviceswhen administering a linux system, it is important to understand how to control and monitor system services. systemd, the system and service manager for most linux distributions, provides a powerful set of tools for this purpose.
- Global variables (global), local variables (local), nonlocal variables in Pythonin this python lesson you will learn about global variables, local variables, nonlocal variables in python and the use of these variables.
- PHP for PERL Programmersthis chapter will list some of the major similarities and differences between php and perl. this helps perl programmers understand php faster and avoid common errors.
- Microsoft officially submitted an application to join 'Linux secretion'.if they are included in this 'secret society', microsoft will have early access to linux-related security vulnerabilities.