What are Environment Variables in Linux? Everything you need to know

If you’ve been using Linux for a long time, you may be wondering how to set certain parameters from the command line that can keep your settings across programs. Environment variables are how you do this.

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 should be consistent across all instances of the Linux shell. When you start a program from the shell, it makes a copy of itself, or “forks”, and then replaces itself with the program that will run it, known as “exec”.

Environment variables are inherited by all copies of the shell that the shell creates, also known as “exporting” 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 preference menus for configuration.

An environment variable could define your preferred editor. So when a program starts the text editor to change a configuration file, this editor is displayed every time.

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. Conventionally, they are printed in capital letters and identified by a leading “$” character, for example “$ EDITOR” for the standard text editor.

List environment variables

To view the value of an environment variable, use the echo Command. To the exampleto view the value of the variable $ EDITOR, run the following command:

echo $EDITOR

If it is set the shell will print the value, for example “vim”, but if it is blank it will just print a blank line.

To display all currently set environment variables, enter “to adjust“on the command line.

Set environment variables on Linux

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

The first method is easy. In bash use the “export“Command. For exampleto set the $ EDITOR Environment variable:

export EDITOR='vim'

Note that in this syntax you use the leading “$“. You can also use VARIABLE = ‘value’, but that only extends to that particular instance of the shell, which means the shell will reset the changed value as soon as you do close The terminal.

Now your editor is Vim or any other text editor that you use in that session and any subshell that launches it. If you want to keep your environment variables between different shell sessions, set them in your shell’s startup files.

Bash reads multiple files at startup: / etc / profile, /Etc/bash/ bashrc, .bash_profile, and .bashrc. The first two are system-wide and only a system administrator can change them while the rest are in your home directory.

the .bashrc File is what you want to edit as 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 read.

The method of setting environment variables in a file is the same as setting it on the command line. Just attach it export VARIABLE = ‘value’ Command 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 this, you’ll have to edit your shell’s configuration file instead of .bashrc, this is the configuration file for bash.

Now you know how environment variables work

Environment variables allow you to use consistent settings for all of your shell sessions. Shell variables are an important part of shell scripting, something every Linux user should know.

Related Posts