What are environment variables in Linux? All you need to know


If you’ve been using Linux for a while, you might be wondering how to set some parameters from the command line that can keep your settings in all programs. Environment variables are the way you do it.


By the end, you will have a deep understanding of what environment variables are and how you can create such variables from the command line.


What are environment variables?

Environment variables are variables that are supposed to be consistent across all instances of the Linux shell. When you run a program from the shell, it makes a copy of itself, or “forks”, then replaces itself with the program it is going to run, called “exec”.

Environment variables will be inherited in all copies of the shell that the shell generates, also known as “export” variables. Even Windows uses environment variables, dating back to the days of MS-DOS, but in modern times most programs rely on the registry and their own settings menus for configuration.

An environment variable can set your preferred editor, so when a program launches the text editor to edit a config file, that editor appears every time.

VIDEO OF THE DAY MAKEUSE

Environment variables can be set system-wide by the system administrator, but it is more common for them to be set in startup files for individual users. By convention, they are printed in upper case and are indicated by a leading “$” sign, such as “$ EDITOR” for the default text editor.

List of environment variables

To see the value of any environment variable, use the echo order. For example, to display the value of the variable $ EDITOR, run the following command:

echo $EDITOR

If set, the shell will print the value, such as “vim”, but if it is empty it will just print an empty line.

To see all currently set environment variables, type “together“on the command line.



run the set command

Defining environment variables in Linux

You can set environment variables in two ways: from the command line or in shell configuration files.


The first method is simple. In Bash, you use the “export“. For example, to define the $ EDITOR environment variable :

export EDITOR='vim'

Note that in this syntax, you omit the beginning “$“. You can also use VARIABLE = ‘value’, but that will just extend to that particular instance of the shell, which means that the shell will reset the modified value once you close the terminal.

Now your editor will be Vim or whatever text editor you have in this session and every subshell it launches. If you want to keep your environment variables between different shell sessions, set them in your shell startup files.

Bash reads several files on startup: / etc / profile, / etc / bash / bashrc, .bash_profile, and .bashrc. The first two are system-wide and can only be changed by a system administrator, while the others reside in your home directory.


The .bashrc is what you want to change, because it affects interactive shells whether or not it is running as a login shell. If you are using the shell of a terminal emulator, .bashrc is what will be read.

The method of setting environment variables in a file is the same as setting on the command line. Just add the export VARIABLE = ‘value’ in the shell configuration file. When you’re done, save the file and start a new shell.

Note that Linux users can also change the default shell from Bash to another shell. For those who have it, you will need to edit your shell config file instead of .bashrc, which is the Bash configuration file.

You now know how environment variables work

With environment variables, you can have consistent settings across all of your shell sessions. Shell variables are an important part of shell scripts, something every Linux user should be aware of.



shell script

What is Shell Script and Why You Should Use It

In addition to being able to accept and execute commands interactively, the shell can also execute commands stored in a file. This is called shell scripts. Here we cover the basics of shell scripts.

Read more


About the Author


Comments are closed.