Setting env variable in linux

EnvironmentVariables

Environment variables provide a way to influence the behaviour of software on the system. For example, the «LANG» environment variable determines the language in which software programs communicate with the user.

Environment variables consist of names that have values assigned to them. For example, on a typical system in the US we would have the value «en_US.UTF-8» assigned to the «LANG» variable.

The meaning of an environment variable and the format of its value are determined by the application using it. There are quite a few well-known environment variables for which the meaning and the format have been agreed upon and they are used by many applications.

Manipulating environment variables and values

While quite a few graphical system configuration applications actually manipulate environment variables in the background, the command-line allows for maximum flexibility when manipulating environment variables.

Note: The shell techniques explained in the following sections apply to the Bourne Shell family of command line shells, which includes sh, ksh, and bash, which is the default shell shipped with Ubuntu. The commands may be different on other shells such as csh.

Setting values to environment variables

In order to set a value to an existing environment variable, we use an assignment expression. For instance, to set the value of the «LANG» variable to «he_IL.UTF-8», we use the following command:

If we use an assignment expression for a variable that doesn’t exist, the shell will create a shell variable, which is similar to an environment variable but does not influence the behaviour of other applications.

A shell variable can be exported to become an environment variable with the export command. To create the «EDITOR» environment variable and assign the value «nano» to it, you can do:

The bash shell (the default command-line shell in Ubuntu) provides a shortcut for creating environment variables. The previous example could be performed with the following single command:

Examining values of environment variables

The printenv command prints the names and values of all currently defined environment variables:

To examine the value of a particular variable, we can specify its name to the printenv command:

Another way to achieve that is to use the dollar sign ($), as used in the following example:

There is a command for doing temporary, short-term changes to the environment. It can also be used to display the current environment. This command is env.

The dollar sign can actually be used to combine the values of environment variables in many shell commands. For example, the following command can be used to list the contents of the «Desktop» directory within the current user’s home directory.

Читайте также:  Moving files and folders in linux

For the sake of completeness: If you want to print the names and values also of the non-exported shell variables, i.e. not only the environment variables, this is one way:

Desktop environment specifics

If you use a terminal window to examine environment variables, you actually study the environment variables of the shell that is running in the terminal. Those variables are not necessarily available in the graphical environment of the desktop. To examine the environment variables that are effective when you for instance start an application from the launcher, you can follow the guidance in this Ask Ubuntu answer.

Erasing environment variables

While simply setting an empty value to an environment variable, as shown in the example below, may nullify its effect in most cases, there are a few variables such as «POSIXLY_CORRECT» whose mere existence, even with an empty value, influences the behavior of programs.

The unset command can be used in order to completely erase the existence of an environment variable:

It is also possible to use the «-n» switch to the export command in order to un-export an environment variable and therefore demote it to become a shell variable while preserving its value.

Working principles of environment variables

A few simple principles govern how environment variables work and achieve their effect.

Process locality

The values of environment variables are local, which means they are specific to the running process in or for which they were set. This means that if we open two terminal windows (which means we have two separate bash processes running), and change a value of an environment variable in one of the windows, that change will not be seen by the shell in the other window or any other program currently on the desktop.

Inheritance

When a parent process creates a child process, for example when we run the «gedit» command from the terminal and «bash» (the parent process) creates «gedit» (the child process), the child process inherits all the environment variables and values the parent process had.

This means that if we set a new value to the «LANG» environment variable in the terminal, and then run «gedit» from that same terminal , «gedit» will inherit the new value of «LANG», and therefore may display in a different language than the rest of the processes on the desktop. (See The LANGUAGE priority list, though.)

Note that because of the Process Locality principle explained above, once we run Gedit, changes to environment variables of the parent process will not be seen by the child process and vice-versa.

Note: in the Gnome graphical desktop environment, gnome-session is the parent process of all the processes running on the desktop. This fact (along with the Inheritance principle) is the key to our ability to powerfully influence the operation of our desktop with environment variables. The equivalent process in KDE is kde-session.

Case sensitivity

The names of environment variables are case sensitive. This means that lang is not the same variable as LANG, Lang, or laNg.

Читайте также:  Linux jq get value

It is a common practice to name all environment variables with only English capital letters and underscore (_) signs.

Bash’s quick assignment and inheritance trick

The bash shell has a trick to allow us to set one or more environment variables and run a child process with single command. For example, in order to set the «LANGUAGE» and «FOO» environment variables and then run «gedit», we would use the following command:

Note: When using this command, the new values are only assigned to the environment variables of the child process (in this case gedit). The variables of the shell retain their original values. For instance, in the example above, the value of «LANGUAGE» will not change from its original value, as far as subsequent commands to the shell are concerned.

A similar behaviour can be achieved with other shells by using the env command.

Persistent environment variables

So far we’ve only discussed ways set an environment variable value temporarily until the shell session in which it was set is closed. One may wonder if there is a way to somehow permanently set an environment variable to a certain value.

Session-wide environment variables

Suitable files for environment variable settings that should affect just a particular user (rather than the system as a whole) are ~/.pam_environment and ~/.profile. After having edited one of those files, you should re-login in order to initialize the variables.

~/.pam_environment

This file is specifically meant for setting a user’s environment. It is not a script file, but rather consists of assignment expressions, one per line. This example sets the variable FOO to a literal string and modifies the PATH variable:

FOO=bar PATH DEFAULT=$:/home/@/MyPrograms
  • When doing a simple variable assignment like the FOO=bar example, quotes have not special meaning. This means that values cannot contain spaces.
  • The syntax used for modifying PATH, which differs from the syntax of shell script files, is required for variable expansion to work. Some variables, like HOME, might not be set at the time ~/.pam_environment is parsed. See /etc/security/pam_env.conf for more details.
  • ~/.pam_environment is written to when you use various GUIs to set the language or regional formats. Consequently, if you for instance set LC_TIME by editing ~/.pam_environment manually, your entry will be overwritten if you afterwards use the Language Support GUI to change the regional formats setting.

~/.profile

In this file you can also place environment variable assignments, since it gets executed automatically by the DisplayManager during the start-up process desktop session as well as by the login shell when one logs in from the textual console. This is a ~/.profile equivalent of the above example:

export FOO=bar export PATH="$PATH:$HOME/MyPrograms"

Note: The code in ~/.profile is run after ~/.pam_environment has been read. That makes ~/.profile suitable to use if you want to override a locale related variable that was set in ~/.pam_environment via e.g. Language Support.

Читайте также:  Кали линукс wifi драйвер

Other files

Shell config files such as ~/.bashrc, ~/.bash_profile, and ~/.bash_login are often suggested for setting environment variables. While this may work on Bash shells for programs started from the shell, variables set in those files are not available by default to programs started from the graphical environment in a desktop session.

System-wide environment variables

A suitable file for environment variable settings that affect the system as a whole (rather than just a particular user) is /etc/environment. An alternative is to create a file for the purpose in the /etc/profile.d directory.

/etc/environment

This file is specifically meant for system-wide environment variable settings. It is not a script file, but rather consists of assignment expressions, one per line.

Note: Variable expansion does not work in /etc/environment.

/etc/profile.d/*.sh

Files with the .sh extension in the /etc/profile.d directory get executed whenever a bash login shell is entered (e.g. when logging in from the console or over ssh), as well as by the DisplayManager when the desktop session loads.

You can for instance create the file /etc/profile.d/myenvvars.sh and set variables like this:

export JAVA_HOME=/usr/lib/jvm/jdk1.7.0 export PATH=$PATH:$JAVA_HOME/bin

Other files

While /etc/profile is often suggested for setting environment variables system-wide, it is a configuration file of the base-files package, so it’s not appropriate to edit that file directly. Use a file in /etc/profile.d instead as shown above. (Files in /etc/profile.d are sourced by /etc/profile.)

/etc/default/locale is specifically meant for system-wide locale environment variable settings. It’s written to by the installer and when you use Language Support to set the language or regional formats system-wide. On a desktop system there is normally no reason to edit this file manually.

The shell config file /etc/bash.bashrc is sometimes suggested for setting environment variables system-wide. While this may work on Bash shells for programs started from the shell, variables set in that file are not available by default to programs started from the graphical environment in a desktop session.

sudo caveat

Defaults env_keep += "http_proxy SOMEOTHERVARIABLES ANOTHERVARIABLE ETC"

Launching desktop application with an environment variable

You can add an environment variable to an application by editing its .desktop file. For example, to run «digiKam» with the environment variable APPMENU_DISPLAY_BOTH=1, find the corresponding digikam.desktop file and add the setting of the variable, via the env command, to the entry «Exec»:

Exec=env APPMENU_DISPLAY_BOTH=1 digikam -caption "%c" %i

List of common environment variables

Each application is free to define and use its own environment variables. Many manual pages include long lists of environment variables that can affect the behaviour of the application they describe. However, the most useful variables are common to many applications.

The following variables determine how the system locates various files in order to operate.

Источник

Оцените статью
Adblock
detector