Bash environment variables. Environment Variables in Linux

Environment Variables (environment variable) are used for storage general values variables within different scripts and programs. Such variables can be set for a time, for example, for the period of operation of a specific terminal shell, or for the period of a user session, or you can set an environment variable at the global level - for the entire system.

Environment Variables

$HOME
The variable contains the path to the current user's home directory. $USER
Current username $PATH
List of directories for the shell to search for executable programs. $PWD
Path to the current working directory (or pwd). Example: dir_path=$(pwd) . $SHELL
Default interpreter $RANDOM
Generates a random number 0..32767 each time a variable is accessed. $SECONDS
Time in sec. from the moment you start the command shell. $? The result of the previous command. $HOSTNAME
Computer name $HISTFILE
Path to the interpreter history file $IFS
List of command separator characters, parameters, array elements (default: space, tab, new line) $PS1
Interpreter prompt string template.

Temporarily setting an environment variable

Setting an environment variable for the session period:

# For a new process env varname=value [command] # For the current shell and all its subprocesses export varname=value [command]

The variable's value is retained until the system is rebooted.

Setting a constant value

System-level environment variables are set through the /etc/environment file:

ENVIRONMENT="dev"

Setting a user-specific environment variable via the ~/.bashrc or ~/.bash_profile file:

PATH="$(PATH):/home/user/bin:"

Attention!

The ~/.bash_profile file will be executed when the interpreter is started with the -l switch. This file cannot be read when logged in locally!

You also need to remember that the changes will only take effect after rebooting the session!

Viewing the value of an environment variable

To view the value of an environment variable, there is the printenv command:

Printenv<имя_переменной>

#shell, #bash, #environment

Original: Linux Fundamentals
Author: Paul Cobbaut
Published date: October 16, 2014
Translation: A. Panin
Translation date: December 13, 2014

Chapter 12. Shell Variables

In this chapter, we will learn how to work with environment variables using the command shell. These variables are typically required for applications to function.

Dollar symbol ($)

Another important symbol interpreted by the shell is the dollar symbol $. The command shell will look for an environment variable with a name corresponding to the line after the dollar sign, and replace that symbol and the variable name with the value of that variable (or with nothing if the variable does not exist).

Below are some examples of using the $HOSTNAME, $USER, $UID, $SHELL and $HOME variables. $ echo This is the shell $SHELL This is the /bin/bash shell $ echo This is the $SHELL shell used on the computer $HOSTNAME This is the /bin/bash shell used on the computer RHELv4u3.localdomain $ echo The user ID $USER is equal to $UID The user ID paul is equal to $500 echo My home directory is $HOME My home directory is /home/paul

Case sensitivity

This example shows that shell variable names are case sensitive! $ echo Hello $USER Hello paul $ echo Hello $user Hello

Creating Variables

This example creates the $MyVar variable and then sets its value. The example then uses the echo command to check the value of the created variable. $ MyVar=555 $ echo $MyVar 555 $

Quotes

Note that double quotes also allow variable expansion on the command line, while single quotes prevent such disclosure. $ MyVar=555 $ echo $MyVar 555 $ echo "$MyVar" 555 $ echo "$MyVar" $MyVar

The bash shell will replace variables with their values ​​in double-quoted lines, but will not do so in single-quoted lines. paul@laika:~$ city=Burtonville paul@laika:~$ echo "We are now in $city." Now we are in the city of Burtonville. paul@laika:~$ echo "We are now in $city." We are now in $city.

set command

You can use the set command to list environment variables. On Ubuntu and Debian systems, the set command will also list shell functions after the list of shell variables. Therefore, to familiarize yourself with all elements of the list of environment variables when working with these systems, it is recommended to use the set | more.

unset command

You should use the unset command to remove a variable from your shell environment. $ MyVar=8472 $ echo $MyVar 8472 $ unset MyVar $ echo $MyVar $

Environment variable $PS1

The $PS1 environment variable sets the greeting format for your shell. When entering a format string, you can use backslash to escape such special characters, like the \u character to display the username, or \w to display the working directory name. The bash shell man page provides full list special characters.

In the example below, we change the value of the $PS1 environment variable several times. paul@deb503:~$ PS1=invitation invitation invitationPS1="invitation " invitation invitation PS1="> " > > PS1="\u@\h$ " paul@deb503$ paul@deb503$ PS1="\u@\h :\W$" paul@deb503:~$

In order to avoid uncorrectable errors, you can use green color for shell prompts displayed to standard users, and red for shell prompts displayed to the root user. Add the following lines to your .bashrc file to use green in the prompts displayed to regular users. # colored shell prompt created by paul RED="\[\033" WHITE="\[\033" GREEN="\[\033" BLUE="\[\033" export PS1="$(debian_chroot:+( $debian_chroot))$GREEN\u$WHITE@$BLUE\h$WHITE\w\$ "

$PATH environment variable

The $PATH environment variable sets the directories file system, in which the shell searches for the binary files needed to execute commands (unless the command is a builtin or represented by a command alias). This variable contains a list of directory paths separated by colon characters. [$ echo $PATH /usr/kerberos/bin:/usr/local/bin:/bin:/usr/bin:

The command shell will not search the current directory for executable binaries. (Search function executable files in the current directory was the simplest mechanism for unauthorized access to data stored on computers running PC-DOS). If you want the shell to search for executable files in the current directory, you should add the symbol. to the end of the line that is the value of your shell's $PATH variable. $PATH=$PATH:. $ echo $PATH /usr/kerberos/bin:/usr/local/bin:/bin:/usr/bin:. $

The value of your shell's $PATH variable may be different if you use the su command instead of the su - command, since the latter command allows additional use of the target user's environment variable values. For example, in the list of directories represented by the value of the $PATH variable root user usually the /sbin directories are added. $ su Password: # echo $PATH /usr/local/bin:/bin:/usr/bin:/usr/X11R6/bin # exit $ su - Password: # echo $PATH /usr/local/sbin:/usr/ local/bin:/sbin:/bin:/usr/sbin:/usr/bin: #

env command

The env command, when used without parameters, will display a list of exported environment variables. The difference between this command and the set command with parameters is that the set command lists all environment variables, including those variables that are not exported to child shells.

Additionally, the env command can also be used to launch a "pure" shell (a shell without inheriting any environment). The env -i command allows you to clear the environment of a child shell.

By revising this example Note that the bash shell will set the $SHELL environment variable on startup. $ bash -c "echo $SHELL $HOME $USER" /bin/bash /home/paul paul $ env -i bash -c "echo $SHELL $HOME $USER" /bin/bash $

You can use the env command to set the value of the $LANG variable or any other environment variable of a single bash shell instance within a single command. In the example below, this feature is used to demonstrate the influence of the value of the $LANG variable on the operation of the file pattern search mechanism (to obtain additional information For information about this mechanism, please refer to the chapter on searching for files using templates). $ env LANG=C bash -c "ls File" Filea Fileb $ env LANG=en_US.UTF-8 bash -c "ls File" Filea FileA Fileb FileB $

export command

You can export shell variables to other shells using the export command. In the example below, this command exports an environment variable to child shells. $ var3=three $ var4=four $ export var4 $ echo $var3 $var4 three four $ bash $ echo $var3 $var4 four

However, using this command, the variable is not exported to the parent command shell (the previous example is continued below). $ export var5=five $ echo $var3 $var4 $var5 four five $ exit exit $ echo $var3 $var4 $var5 three four $

Variable delimitation

Until now, we have seen that the bash shell interprets a variable starting with a dollar sign and continuing until the first non-alphanumeric character that is not an underscore appears. In some situations this behavior can be problematic. To solve this problem, curly braces can be used in the manner shown in the example below. $ prefix=Super $ echo Hello $prefixman and $prefixgirl Hello and $ echo Hello $(prefix)man and $(prefix)girl Hello Superman and Supergirl $

Unbound Variables

The example below attempts to display the value of the $MyVar variable, but is unsuccessful due to the fact that the variable does not exist. By default, the shell will not print anything if the variable is not bound (it does not exist). $ echo $MyVar$

However, there is a nounset shell option that you can use to generate an error if the variable you are using does not exist. paul@laika:~$ set -u paul@laika:~$ echo $Myvar bash: Myvar: unbound variable paul@laika:~$ set +u paul@laika:~$ echo $Myvar paul@laika:~$

In the bash shell, the set -u command is identical to the set -o nounset command and, similarly, the set +u command is identical to the set +o nounset command.

Practice: Shell Variables

2. Create a variable answer whose value is 42.

3. Copy the value of the $LANG variable to the value of the $MyLANG variable.

4. List the shell variables currently in use.

5. List all exported shell variables.

6. Is information about your variable present in the output of the env and set commands?

7. Destroy your answer variable.

8. Create two variables and export one of them.

9. Print the value of the exported variable in the child interactive command shell.

10. Create a variable and assign it the value "Dumb", then create another variable with the value "do" in the same way. Use the echo command and the two created variables to print the word "Dumbledore".

11. Find a list of backslash-escaped control characters in the bash shell man page. Add a control character to the PS1 variable to display the time in the shell greeting.

Correct Procedure for Practice: Shell Variables

1. Use the echo command to print the string "Hello" followed by your name. (Use a bash shell variable!)

Environment variables are special variables that are defined in the shell and are needed by programs or scripts at runtime. They can be defined by the system or the user. System-defined variables are those that are set by the system.

For example the command P.W.D. is a very common system variable that is used to store the current working directory. User variables are usually set either temporarily for the current shell or permanently. The whole concept of setting up and changing environment settings revolves around a set of files and a few commands and different shells.

More precisely, an environment variable can be of three types:

1. Local environment variable

It is defined for the current session. These environment variables are used for the duration of the current session, whether it is a remote login session or a local terminal session. These variables are not specified in any configuration files and are created and deleted using a special set of commands.

2. User environment variable

These are variables that are defined for a specific user and are loaded whenever the user logs in using a local terminal session or if that user logs in using a remote login session. These variables are typically set and loaded from the following configuration files: .bashrc,.bash_profile,.bash_login,.profile, which are present in the user's home directory.

3. System environment variables

These are environment variables that are available system-wide, that is, to all users present on that system. These variables are present in system-wide configuration files, present in the following directories and files: /etc/environment,/etc/profile, /etc/profile.d/, /etc/bash.bashrc. These variables are loaded every time the system is turned on and users log in, either locally or remotely.

Understanding General and System-Wide Configuration Files

Here we will briefly describe the various configuration files listed above that contain environment variables, both system and user.

.bashrc

This file is a user file that is loaded every time the user creates a new local session, i.e. in simple words, opens a new terminal. All environment variables created in this file take effect each time a new local session is started.

.bash_profile

The environment variables listed in this file are called every time the user logs in remotely, that is, using the command ssh. If this file does not exist, the system searches for files .bash_login or .profile.

/etc/environment

This file is the system file for creating, editing or deleting any environment variables. Environment variables created in this file are available throughout the system, to each user globally, both locally and remotely.

/etc/bash.bashrc

System file bashrc. This file is downloaded once per user, each time the user opens a local terminal session. Environment variables created in this file are available to all users, but only through a local terminal session. When any user on this computer is removed via a remote login session, these variables will not be visible.

/etc/profile

All variables created in this file are available to every user on the system, but only if that user session is invoked remotely, that is, via remote login. Any variable in this file will not be available to the local login session, i.e. when the user opens a new terminal on his local system.

Note. Environment variables created using system-wide or user configuration files can be removed, but can only be removed from those files. Just after each change to these files, either log out or log in again or simply type the following command on your terminal for the changes to take effect:

$source

Setting or removing local or session environment variables in Linux

Local environment variables can be created using the following commands:

$ var=value OR $ export var=value

These variables are session variables and are valid only for the current terminal session. To clear these environment variables, you can use the following commands:

1.Usage env

By default the command " env» lists all current environment variables. But, if used with the key " -i" then it temporarily clears all environment variables and allows the user to execute the command in the current session in the absence of all environment variables.

$ env –i ... command args ...

Here var = value matches any local environment variable that you want to use only with this command.

$ env -i bash

We launch a bash shell, which will temporarily clear all environment variables. But when you exit the shell, all variables will be restored.

2. Using Cancel

Another way to clear a local environment variable is to use the command unset. To temporarily disable any local environment variable,

$unset

Where, var-name is the name of the local variable you want to remove or clear.

3. Set the variable name using empty value

Other less popular way— assign the name of the variable you want to clear to an empty value, i.e. VAR=(after equals there is nothing - press enter). This will clear the value of the local variable for the current session for which it is active.

NOTE: YOU CAN DO CHANGE SYSTEM VARIABLES, BUT THE CHANGES WILL ONLY BE REFLECTED IN THE CURRENT SESSION AND WILL NOT BE PERMANENT.

Learn how to create, user and system environment variables in Linux

In this section, we will look at how to set or disable local, user and system environment variables in Linux with the examples below:

1. Setting and removing local variables in Linux.

a) Here we create a local variable VAR1 and set it to any value. Then we use unset and at the end we delete this variable.

$ VAR1="TecMint is the best Site for Linux Articles" $ echo $VAR1 $ unset VAR1 $ echo $VAR1

b) Another way to create a local variable is to use the command export. The created local variable will be available for the current session. To disable a variable, simply set the variable to empty.

$ export VAR="TecMint is best Site for Linux Articles" $ echo $VAR $ VAR= $ echo $VAR

c) Here we have created a local variable VAR2 and set its value. Then, to run the command, temporarily clearing all local and other environment variables, we ran the command " env -i" This command here started the shell bash, clearing all other environment variables. After entering " exit" in the called shell bash all variables will be restored.

$ VAR2="TecMint is the best Site for Linux Articles" $ echo $VAR2 $ env -i bash $ echo $VAR2

2. Setting and removing user environment variables in Linux.

a) Change the file .bashrc in your home directory to export or set the environment variable to be added. After that, run the file for the changes to take effect. Then you will see the variable ("CD" in my case) that has taken effect. This variable will be available every time you open a new terminal for that user, but not for remote login sessions.

$vi.bashrc

.bashrc.

Export CD="This is TecMint Home"

Now run the following command to make the new changes take effect and test the variable.

$source.bashrc $echo $CD

To remove this variable, simply delete the line in the file. bashrc and re-enter the shell.

b) To add a variable that will be available for remote login sessions, modify the file .bash_profile.

$vi.bash_profile

Add the following line to the file .bash_profile.

Export VAR2="This is TecMint Home"

The variable will be available when you execute remote using ssh login for that user, but not while opening a new local terminal.

$source.bash_profile $echo $VAR2

Here VAR2 initially not available, but when performing a remote login using ssh user on localhost the variable becomes available.

$ssh$echo$VAR2

To remove this variable, simply delete the line in the file .bash_profile that you added and re-read the file.

NOTE: These variables will be available on every login for the current user, but not for other users.

3. Setting and removing system environment variables in Linux.

a) To add a non-login system variable (that is, one that is available to all users when any of them opens a new terminal, but not when there is remote access any user to the machine) add this variable to the file /etc/bash.bashrc.

Export VAR="This is system-wide variable"

After that, re-read the file.

$ source /etc/bash.bashrc

Now this variable will be available to each user when he opens a new terminal.

$ echo $VAR $ sudo su $ echo $VAR $ su - $ echo $VAR

Here the variable is available to both the root user and regular user. You can check this by logging in as a different user.

b) If you want some environment variable to be available when any of the users on your computer logged in remotely but did not open any new terminal on local computer, you need to edit the file − /etc/profile .

Export VAR1="This is system-wide variable for only remote sessions"

After adding the variable, simply re-read the file. Then the variable will be available.

$source/etc/profile $echo $VAR1

To remove this variable, delete the line from the file /etc/profile and re-read it.

c) However, if you want to add a variable to any environment where you want to be available system wide, for both remote login sessions and local sessions for all users, simply export the variable to /etc/environment.

Export VAR12="I am available everywhere"

After that, just re-read the file and the changes will take effect.

$ source /etc/environment $ echo $VAR12 $ sudo su $ echo $VAR12 $ exit $ ssh localhost $ echo $VAR12

Here, we can see that the environment variable is available for the normal user, the root user, and also for the remote login session.

To clear this variable, simply delete the entry in the file /etc/environment and reread the file.

NOTE: Changes take effect when the file is reread /etc/environment. But, if not, you may need to log out and log in again.

Ask questions about the article in the comments below.

Thanks for taking the time to read the article!

If you have any questions, ask them in the comments.

Subscribe to our blog updates and stay up to date with news from the world of infocommunications!

To know more and stand out among the IT crowd with knowledge, sign up for Cisco courses from the Cisco Academy, Linux courses from the Linux Professional Institute

  • We will ask you about a convenient time for practice and adjust: we understand that there is little time to study.
  • If you want an individual schedule, we will discuss and implement it.
  • We will set clear deadlines for self-organization. A personal supervisor will be in touch to answer questions, advise and motivate you to adhere to exam deadlines.
  • We will also help you:

    Linux environment variables, along with shell variables, are what determine settings within a user session.

    They are loaded automatically, but can be overridden. The environment is created every time you log into the system.

    Linux Environment Variables, Shell Variables

    Environment Variables are used for the user shell process and all child processes spawned within it.

    Shell Variables contain data that defines the state current session. For example, the current directory.

    Each session uses both environment variables and shell variables.

    The list of environment variables can be viewed by running printenv or env

    ……
    LC_MEASUREMENT=ru_RU.UTF-8
    LESSCLOSE=/usr/bin/lesspipe %s %s
    LC_PAPER=ru_RU.UTF-8
    LC_MONETARY=ru_RU.UTF-8
    LANG=en_US.UTF-8
    ……

    The difference is that printenv allows you to display data for a specific variable - for example:

    The advantage of env is that it makes it possible to pass a variable to a specific command

    For example:

    env SHELL=»/bin/bash» ls -la /opt

    View environment variables

    BASH=/bin/bash
    BASHOPTS=checkwinsize:cmdhist:complete_fullquote:expand_aliases:extglob:extquote:force_fignore:histappend:interactive_comments:progcomp:promptvars:sourcepath
    BASH_ALIASES=()
    BASH_ARGC=()
    ……

    The output can be redirected to less to make it easier to navigate

    In order to get rid of unnecessary information in the output you can do this

    In POSIX mode, bash defined functions will not be printed

    The most frequently used variables:
    • SHELL- a shell that interprets commands
    • USER— user
    • P.W.D.- current directory
    • TERM— the type of terminal that is emulated

    Shell commands allow you to change the value of variables. So pwd displays the current directory to the console

    /home/admin

    This is a PWD shell variable

    admin@desktop:~$ set | grep PWD

    OLDPWD=/tmp
    PWD=/home/admin

    Let's go to /tmp and see how the value changes

    admin@desktop:~$ cd /tmp/

    admin@desktop:/tmp$ set | grep PWD

    OLDPWD=/home/admin
    PWD=/tmp

    You can see that when you change the directory, the value of the shell variable PWD simply changes, and the OLDPWD

    How to set an environment or shell variable

    The shell variable is added like this

    SOMEVAR="Hi there"

    It will not be in the env output, because it is a shell variable, not an environment variable.

    You can view the contents via echo by adding a $ sign before the name

    Hi there

    To add a variable to the environment, you need to export it

    export SOMEVAR=”Hi there”

    SOMEVAR=Hi there

    After this, the value will be inherited by child processes.

    You can delete an environment variable using the same export command with the key -n

    The value is removed from the shell unset

    Automatic loading of variables upon login

    Linux systems use files ~/.bashrc, ~/.bash_profile, ~/.bash_login or ~/.profile to define variables. The files are read during login, which one is used depends on the distribution.

    On Ubuntu/Debain this is ~/.bashrc. It adds both environment variables and shell variables.

    The very first one is read /etc/profile. It specifies and can be overridden which files to use next and in what order.

    For a non-login session, variables are set in /etc/bash.bashrc. This session is typical for scripts running in the background.

    For most tasks it is enough to edit ~/.bashrc.

    Environment variables in Linux are special variables defined by the shell and used by programs at runtime. They can be defined by the system and the user. Linux system environment variables are defined by the system and used by system-level programs.

    For example, the PWD command uses a system variable to keep the previous working directory. User environment variables are set by the user, for the current shell, either temporarily or permanently. The whole concept of adding and removing shell variables revolves around multiple files, commands, and different shells.

    More broadly, an environment variable can be of three types:

    1. Local environment variables

    These variables are defined only for the current session. They will be permanently erased after the session ends, be it remote access or a terminal emulator. They are not stored in any files, but are created and deleted using special commands.

    2. Custom Shell Variables

    These shell variables in Linux are defined for a specific user and are loaded each time the user logs in using a local terminal or connects remotely. Such variables are typically stored in configuration files: .bashrc, .bash_profile, .bash_login, .profile or other files located in the user directory.

    3. System environment variables

    These variables are available throughout the system, for all users. They are loaded at system startup from system files configurations: /etc/environment, /etc/profile, /etc/profile.d/ /etc/bash.bashrc.

    Linux Environment Variable Configuration Files

    Here we will briefly look at the various configuration files, listed above, which are used to set environment variables for the entire system or a specific user.

    .bashrc

    This is a user-specific variable file. Loaded every time the user creates a terminal session, that is, in other words, opens a new terminal. All environment variables created in this file take effect every time a new terminal session is started.

    .bash_profile

    These variables take effect every time the user connects remotely via SSH. If this file is missing the system will look for .bash_login or .profile.

    /etc/environment

    This file is for creating, editing and deleting any environment variables at the system level. Environment variables created in this file are available for the entire system, for each user, and even when connecting remotely.

    /etc/bash.bashrc

    System bashrc. This file is executed for each user, every time he creates a new terminal session. This only works for local users, when connected via the Internet, such variables will not be visible.

    /etc/profile

    System file profile. All variables from this file are available to any user on the system only if he is logged in remotely. But they will not be available when creating a local terminal session, that is, if you simply open the terminal.

    All Linux environment variables created using these files can be removed simply by deleting them from there. Only after each change, you need to either log out and log back in, or run this command:

    source filename

    Adding User and System Environment Variables in Linux

    Now that you know a little theory, let's move on to practice. Local environment variables in Linux can be created with the following commands:

    var=value
    $ export var=value

    These variables will only be available for the current terminal session.

    There are several commands you can use to remove environment variables:

    1. Using env

    By default, you can use env to view all set environment variables. But with the -i option, it allows you to temporarily remove all shell variables and execute the command without variables.

    env –i command

    Var is any variable you want to pass to this command.

    This command will start the shell without any environment variables at all:

    After launching such an environment, no variables will be available, but after exiting everything will return to its place.

    2. Using unset

    This is another way to remove Linux environment variables. Unset removes a variable by name until the end of the current session:

    unset variable_name

    3. Set the variable value to ""

    This is the easiest way to remove environment variables in Linux; by setting a variable to empty, you remove it for the rest of the current session.

    Note: Using these methods you can change the values ​​of system or user variables, but they will only be relevant for the current session.

    Creating User and System Environment Variables

    In this section, we'll look at how to set and delete system and user variables not only for the current session, but so that the effect persists after a reboot.

    1. Set and remove local variables in Linux

    Let's create a local variable VAR and set it to any value, then unset it and make sure it is deleted:

    VAR1="Losst"
    $ echo $VAR1
    $unset VAR1
    $ echo $VAR1

    Another way to create a variable is with the export command. Let's remove it by assigning an empty value:

    export VAR="Losst"
    $ echo $VAR
    $VAR=
    $ echo $VAR

    Now let's create a variable VAR2 and give it a value. And then temporarily remove all local variables by running env -i. It will start a shell without any variables. After entering exit, all variables will be restored.

    VAR2="Losst"
    $ echo $VAR2
    $ env -i bash
    $ echo $VAR2

    Setting and Removing User Variables

    Edit the .bashrc file in your home directory by adding an export command to export the desired variable. Then run the source command to apply the changes. Let's create, for example, the CD variable:

    Add this line (o, then paste, then Esc and :wq):

    export CD="This is Lost Home"

    Now it remains to update the configuration:

    source.bashrc
    $echo $CD

    To remove this variable, simply remove it from .bashrc.

    Now let's add an environment variable using .bash_profile. This variable, as you already know, will only be available during remote login:

    vi .bash_profile

    Add the line:

    export VAR2="This is Lost Home"

    And run these commands to apply the changes and check that the variable has been added:

    source.bash_profile
    $ echo $VAR2

    The variable is not available because you have created a local terminal session, now connect via ssh:

    ssh user@localhost
    $ echo $VAR2

    You can delete this environment variable in the same way as in the previous case by deleting it from the file.

    Comment: These variables are always available, but not for all users.

    Setting and removing system environment variables

    Let's create a variable available to all users in all terminal sessions except remote ones by adding it to /etc/bash.profile:

    vi /etc/bash.profile

    export VAR="This is system-wide variable"

    Then we update:

    source /etc/bash.bashrc

    Now this variable is available to all users, in all terminals:

    echo $VAR
    $sudo su
    $ echo $VAR
    $su -
    $ echo $VAR

    If you want to make an environment variable available to all users who connect to this machine remotely, edit the /etc/profile file:

    export VAR1="This is system-wide variable for only remote sessions"

    Update the configuration and check the availability of the variable, it will only be available remotely:

    source /etc/profile
    $ echo $VAR1

    If you need to add an environment variable in Linux so that it is accessible both remotely and for local sessions, export it to /etc/environment:

    vi /etc/environment

    export VAR12="I am available everywhere"

    We check:

    source /etc/environment
    $ echo $VAR12
    $sudo su
    $ echo $VAR12
    $ exit
    $ssh localhost
    $ echo $VAR12

    Internet