Last Updated:

Environment variables in Linux

Environment variables in Linux are a set of named values used by other applications.

Environment variables are used to customize the behavior of applications and the operation of the system itself. For example, an environment variable may store information about executable file paths, a default text editor, a browser, system locale settings, or keyboard layout settings.

 

Environment variables and shell variables

 

Variables can be divided into two main categories:

   Environment variables (or "environment variables") are system-wide variables that are inherited by all child processes and shells.

   Shell variables are variables that apply only to the current shell instance. Each shell, such as bash or zsh, has its own set of internal variables.

All variables have the following format:

KEY=value1
KEY="Some other value"
KEY=value1:value2

In this case, you should also adhere to certain rules:

 Variable names are case-sensitive, so environment variables must be uppercase.

 When assigning multiple values to a variable, they must be separated by the character .:

 There should be no spaces around the character.=

There are several commands with which you can interact with environment and shell variables:

   env command — allows you to run another program in the user environment without changing the current environment. When used without an argument, it will display a list of variables in the current environment;

   printenv command — displays a list of all environment variables (or some separately specified variable);

   set command — sets shell variables. When used without an argument, displays a list of all variables, including environment and shell variables, as well as shell functions;

   unset command — removes shell variables and environment variables;

   export command — creates an environment variable.

Find and output environment variables

 

 

 

The most commonly used command to output environment variables is . If you pass the name of a variable as an argument to the command, the value of only that variable will be displayed. If you call without arguments, then a line-by-line list of all environment variables will be displayed.printenvprintenv

For example, to display the value of the variable , you must use the command:HOME

$ printenv HOME

As a result, you will see the path to the current user's home directory:

You can also pass several arguments to a command at once, for example:printenv

$ printenv LANG PWD

Variable

If you run the command or without any arguments, they will show a list of all the environment variables:printenvenv

$ printenv

Variables in linux

Below are some of the most common environment variables:

   USER is the current user.

   PWD is the current directory.

   OLDPWD is the previous working directory. Used by the shell to return to the previous directory when executing the .cd -

   HOME is the current user's home directory.

   SHELL is the path to the current user's shell (for example, bash or zsh).

   EDITOR is the default editor. This editor will be invoked in response to the .edit

   LOGNAME is the user name used to log on to the system.

   PATH — paths to the directories in which the commands to be called will be searched. When you run the command, the system will go through these directories in the specified order and select the first of them, which will contain the executable file of the desired command.

   LANG is the current language and encoding settings.

   TERM is the type of the current terminal emulator.

   MAIL is where the current user's mail is stored.

   LS_COLORS — specifies the colors used to highlight objects (for example, different file types will be highlighted with different colors in the command output).ls

The most common shell variables are:

   BASHOPTS is a list of the shell parameters involved, separated by a colon.

   BASH_VERSION is the version of the running bash shell.

   COLUMNS is the number of columns that are used to display the output.

   DIRSTACK is a directory stack to which you can apply the commands and .pushdpopd

   HISTFILESIZE is the maximum number of lines for the command history file.

   HISTSIZE is the number of lines from the command history file that can be stored in memory.

   HOSTNAME is the name of the current host.

   IFS is an internal field separator on the command line (the default is a space).

   PS1 is the appearance of the prompt for entering new commands.

   PS2 is a secondary prompt.

   SHELLOPTS is a shell parameter that you can set by using the .set

   UID is the ID of the current user.

Commands and output only environment variables. If you want to get a list of all variables, including shell variables (and functions), you can use the command :printenvenvset

$ set

examples

The command displays a list of all variables. It's pretty big, so I forwarded the output to the command in advance.more

To find all variables that contain a given string, use the command :grep

$ printenv | grep [VARIABLE_NAME]

The following is an example of finding variables whose name contains the string :USER

Bash Version

You can also use the command to display shell variables. For example, to output the value of a variable to a terminal, you must execute:echoBASH_VERSION

$ echo $BASH_VERSION

Setting shell variables

To create a new shell variable with a name, for example, and a value , just type:NEW_VARp-qc.com

$ NEW_VAR='p-qc.com'

You can verify that the variable was indeed created by using the command :echo

$ echo $NEW_VAR

or

$ set | grep NEW_VAR

Use the command to check if our variable is an environment variable:printenv

$ printenv NEW_VAR

The output of the command was empty, which tells us that the variable we created is not an environment variable.

You can try to output the value of the variable in a new shell, but the output will also be empty:

$ bash -c 'echo $NEW_VAR'

Set environment variables

 

 

 

Use the export command to set environment variables. With this command, we export the specified variable, which will make it visible in all newly launched child command shells. Variables of this type are called external.

To create an environment variable, let's export our newly created shell variable:

$ export NEW_VAR

Check the result, whether we really created the environment variable:

$ printenv NEW_VAR

 

This time, if you try to display the variable in the new shell, get its value:

$ bash -c 'echo $NEW_VAR'

Result:

Ravesli.com

You can also use the following construct to create an environment variable:

$ export MY_NEW_VAR="My New Var"

Note: Environment variables created in this way are only available in the current session. If you open a new shell or log out, all variables will be lost.

 

How do I make environment variables permanent?

If you want the variable to persist after the shell session is closed, you must register it in a special file. You can specify a variable both for the current user and for all users.

To set a persistent environment variable for the current user, open the . bashrc:

$ sudo nano ~/.bashrc

For each variable that you want to make constant, add a line to the end of the file using the following syntax:

export [ИМЯ_ПЕРЕМЕННОЙ]=[ЗНАЧЕНИЕ_ПЕРЕМЕННОЙ]

 

Save and close the file. The changes will be applied after the shell is restarted. If you want to apply the changes during the current session, use the command :source

$ source ~/.bashrc

To set persistent environment variables for all users, create a .sh file in the /etc/profile directory. d:

$ sudo nano /etc/profile.d/[имя_файла].sh

The syntax for adding variables to a file is the same as for a .bashrc file:

 

Save and close the file. The changes will be applied the next time you log on.

Deleting variables

 

 

 

To completely delete a variable of any type, use the unset command:

$ unset NEW_VAR



Conclusion

Environment and shell variables are always present in shell sessions and can be very useful. They allow parent processes to set configuration details for their child processes and are a way to set specific parameters without using separate files.

This provides many advantages in specific situations. For example, some deployment mechanisms rely on environment variables to configure authentication information. Environment variables and shells allow you to store this data not in files that can be viewed by unauthorized persons.

There are many other common scenarios in which you will need to read or change the parameters/data of your environment or shell. Now you know how to do it.