Environment variables are name value pair tokens which provide a way to influence the behaviour of software on a system. In other words Environment variables consist of names that have values assigned to them. Though definition and usage of environment variables are determined by the application using it, there are quite a few well-known environment variables which are standardised and they are used by many applications. In this article we will explore commonly used Environment Variables on Ubuntu 14.04 LTS.
Manipulating environment variables on Ubuntu 14.04 LTS
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
We use an assignment expression to set values to an existing environment variable, or define new shell variable, which can be exported to make them as environment variables.
View values of environment variables
We use following commands to view the values of currently defined environment variables
Erasing environment variables
We use following commands to erase the values of currently defined environment variables
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.
export -n ENV_VAR
Note: Above discussed environment variable are set temporarily until the shell session is closed. In the next section we will explore how to permanently set an environment variable.
Persistent Environment variables
Lets explore permanent Environment Variables:
Session-wide environment variables:
We should modify ~/.pam_environment and ~/.profile for setting environment variables that should affect just a particular user (rather than the system as a whole).
It consists of assignment expressions, one per line, and is used to set a user’s environment. This example sets the variable FOO to a literal string and modifies the PATH variable:
This file is 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. Hence environment variable assignments can be placed in this file.
Please Note: 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
/etc/environment and and ones placed in /etc/profile.d are generally recommended for system wide environment variables.
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.
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:
Note: /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.)
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.
Things to take care while working as sudo
Any variables added to these locations will not be reflected when invoking them with a sudo command, as sudo has a default policy of resetting the Environment and setting a secure path (this behavior is defined in /etc/sudoers). As a workaround, you can use sudo su that will provide a shell with root privileges but retaining any modified PATH variables.
Alternatively you can setup sudo not to reset certain environment variables by adding some explicit environment settings to keep in /etc/sudoers:
Run sudo visudo
Add the following to the bottom:
Defaults env_keep += "http_proxy SOMEOTHERVARIABLES ANOTHERVARIABLE ETC"
Most commonly used Environment Variables:
|PATH||/usr/sbin:/usr/bin:/sbin:/bin||List of directories for the system to search for commands|
|MANPATH||/usr/share/man: /usr/local/man||List of directories for the system to search manual pages in|
|LD_LIBRARY_PATH||/opt/app/oracle/lib||List of directories where the system searches for runtime libraries in addition to those hard-defined by ld and in /etc/ld.so.conf.d/*.conf files|
|TMPDIR||/var/tmp||The directory used for temporary file creation by several programs|
|LANG||The basic language setting used by applications on the system, unless overridden by one of the other locale environment variables|
|BROWSER||/usr/bin/lynx||The name of the user’s preferred web browser. This variable is arguably less common than the rest|
|VISUAL||/usr/bin/gedit||Similar to the “EDITOR” environment variable, applications typically try the value in this variable first before falling back to “EDITOR” if it isn’t set.|
|EDITOR||/usr/bin/nano||The name of the user’s preferred text editor. Used by programs such as the mutt mail client and sudoedit.|
|This variable is used to indicate to graphical applications where to display the actual graphical user interface, the value consists of 3 parts: A host-name followed by a colon (:), a display number followed by a dot (.) and a screen number. The host-name part can be used to have the graphical output sent to a remote machine over the network. It can be omitted when the output is meant for an X server running on the local machine. The display number allows selecting among multiple X servers running on the same machine (Ubuntu uses multiple X servers to enable multiple graphical desktop sessions). Although the screen number is used to select among multiple physical screen that are managed by the same X server, it is rarely set to anything other than “0” nowadays. Manually setting the “DISPLAY” environment variable’s value is rarely needed nowadays since it can be automatically and intelligently adjusted by many applications such as “GDM” and “SSH” when needed.|
|LIBRARY_PATH||/usr/lib/firefox||A list of directories (separated by colons) in which library files should be searched for|
|INCLUDE||/opt/app/src/include||A colon-separated list of directories in which header files should be searched for|
|CPATH||..:$HOME/include: /usr/local/include||A colon-separated list of directories in which header files should be searched for|
|USER||myuser1||The name of the currently logged-in user. This variable is set by the system. You probably shouldn’t change its value manually.|
|LOGNAME||myuser1||Similar to “USER”, some programs prefer to read this variable rather than USER.|
|HOME||/home/myuser1||The location of the currently logged-in user’s home directory.|
|PWD||/home/myuser1/Desktop||The current working directory of the shell.|
|SHELL||/bin/bash||The user’s preferred command-line shell as it is set in the /etc/passwd file.|
|POSIXLY_CORRECT||N/A||If this environment variable exists, regardless of its value, it causes the behaviour of quite a few utilities to more closely match the behaviour defined by the POSIX standard. This typically makes various GNU extensions that make life easier not work, but it may just be what’s needed to make an old UNIX script execute successfully.|
|HOSTALIASES||/etc/host.aliases||The name of a file containing host-name aliases for use with various network programs.|
|TZDIR||/usr/share/zoneinfo||The path of the directory containing time-zone information files. This typically does not need to be set manually, as the system searches for such files in /usr/share/zoneinfo by default.|
|This variable was used by older UNIX systems to specify the system’s time-zone. However, Ubuntu and most other modern systems use the /etc/localtime file for that purpose. This variable can, however, be used to make one user’s particular session display times in a different time-zone than the rest of the system.
The value of this variable can either be the name and offset of a time-zone (as seen in the first example) or the name of a zone-info file in /usr/share/zoneinfo (as seen in the second example).
|TERM||xterm||The name of a terminal information file from /lib/terminfo, this file instructs terminal programs how to achieve things such as displaying color. It may help to fiddle with this variable if you’re trying use an odd terminal emulator program or trying to connect a hardware serial-port terminal emulator and getting undesired results.|
|COLUMNS||80||The number of text columns in the terminal window. Try adjusting this variable if lines don’t seem to wrap properly in the console.|
|LINES||25||The number of text lines on the console window. Try adjusting this variable if you’re getting strange results when scrolling text.|