Objects that are given by names and contain data used by at least one, usually multiple applications are called environment variables. Put simply, an environment variable is nothing more than a variable that has a name and a value.
Values of environment variable could be the default editor that will be used, for example, the location of all files that can be executed in the system or local settings for the system. Beginners of Linux will find this a little difficult but the environment variable is an excellent way of sharing the configuration settings between several applications and processes.
Coreutils package is home to the env and printenv programs. To list all the environment variables that currently have a value, you just type:
In the bash shell, there are two types of environment variables:
- Global variables
- Local variables
Table of Contents
Global environment variables
From the shell session, the global environment variables are visible, and any child processes that the shell spawns.
On the other way, the local variables can be available in the shell only if you create them. This makes global environment variables beneficial in applications that require information from the parent process and spawn child processes.
By default, the Linux system will set certain global environment variables when you start your bash session. The system environment variables every time use all capital letters to distinguish them from normal user environment variables.
Local environment variables
As their name indicate the local environment variables can be view only in the local process in which they are defined. Do not get mixed up though, about local environment variables, they are equally important as global environment variables. Аctually, the Linux system also by default will define standard local environment variables for you. It’s getting tricky when you try to see the list of local environment variables. Unfortunately for us, there isn’t a command that will display only the local environment variables. There is a set of command that displays all of the environment variables set for a specific process. Please note that this also includes the global environment variables.
Setting Environment Variables
You can create your own environment variables straight from the bash shell. In the next section, we will show you how to make your own environment variables and reference them from your interactive shell or shell script program.
Setting local environment variables
Once the bash shell starts (or spawn a shell script), you are allowed to create local variables that are visible inside your shell process. You can assign either a string or a numeric value to an environment variable by using the equals sign we assign the value of the variable:
$ rose_test=testing $ echo $rose_test testing $
Congratulations, you just created your first variable. Just remember that any time you need to reference the value of the rose_test environment variable, just reference it by the name $rose_test.
If you need to assign a string of values that contain spaces, you will need to use a single quotation mark to indicate the beginning and end of the string:
$ rose_test=testing a long string -bash: a: command not found $ rose_test='testing a long string' $ echo $rose_test testing a long string
If you leave it without the single quotation marks, the bash shell presumes that the next character is another command to process.
Note that for the local environment variable I defined, I used lower-case letters, as long as the system environment variables we have seen so far have all used upper-case letters.
In the bash shell, this is a standard convention. If you set up new environment variables, it is not required but recommended to use lower-case letters. This helps to differentiate your personal environment variables from the results of the system environment variables.
NOTE: It’s extremely important that there are no spaces between the environment variable name, the equal sign, and the value. If you put any spaces in the assignment, the bash shell interprets the value as a separate command:
$ rose_test2 = test -bash: rose_test2: command not found $
The set local environment variable within your shell process will be available for use anywhere within your shell process. But, if you spawn another shell, it is not available in the child shell:
$ bash $ echo $rose_test $ exit exit $ echo $rose_test testing a long string $
In this case, I started a child shell. As you can notice, the rose_test environment variable is not available in the child shell (it contains a blank value). After I left the child shell and returned to the original shell, the local environment variable was still available.
Similarly, if you set a local environment variable in a child process after you leave the child process, the local environment variable is no longer available:
$ bash $ rose_test=testing $ echo $rose_test testing $ exit exit $ echo $rose_test $
The rose_test environment variable set in the child shell doesn’t exist when I go back to the parent shell.
Setting global environment variables
The global environment variables are visible from all processes of the child created by the process that sets the global environment variable. The method used to create a global environment variable is to create a local environment variable and then export it to the global environment.
This is accomplished by using the export command:
$ echo $rose_test testing a long string $ export rose_test $ bash $ echo $rose_test testing a long string $
After we use the command export on the local environment variable rose_test, we started a new shell process and viewed the value of the rose_test environment variable. This time, the export command made the environment variable global, so it retained its value.
NOTE: Notice that when you run the command export on a local environment variable, you do not use the dollar sign to reference the variable’s name.
Removing Environment Variables
Obviously, if you can create a new environment variable, it would make sense that you may also remove an existing environment variable. Тhis can be done by using the unset command:
$ echo $rose_test testing $ unset rose_test $ echo $rose_test $
When referencing the environment variable within the unset command, keep in mind not to use the dollar sign.
When dealing with global environment variables, things get a bit tricky. If you’re in a child process and unset a global environment variable, it only applies to the child process. Тhe global environment variable remains available within the parent process:
$ rose_test=testing $ export rose_test $ bash $ echo $rose_test testing $ unset rose_test $ echo $rose_test $ exit exit $ echo $rose_test testing $
In this example, we set a local environment variable called rose_test, then exported it to make it a global environment variable. Then we started a child shell process and checked to make sure that the global environment variable rose_test was still available. Next, while still in the child shell, we used the unset command to remove the global environment variable rose_test, then exited the child shell. Now back in the original parent shell, we checked the rose_test environment variable value, and it is still valid.
Default Shell Environment Variables
There are concrete environment variables that bash shell are using to define the system environment. You can always count these variables to be used on your Linux system. Because the bash shell is a derivative of the original Unix Bourne shell, it also includes environment variables originally defined in that shell.
The next examples are showing that the environment variables the bash shell provides are compatible with the original Unix Bourne shell.
So far the most valuable environment variable in this list is the PATH environment variable.
When you enter a command in the shell CLI (command line interface), the shell should search the system to find the program. The directories it searches looking for commands are defined by the PATH environment variable. On Linux system, the PATH environment variable should be looking something like this:
$ echo $PATH /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin $
In this case, shows that there are eight directories where the shell looks for commands. Each directory is separated by a colon and there is nothing at the end of the PATH variable also the end of the directory listing. You can always add additional directories to the PATH by adding the new directory and adding another colon. The PATH likewise demonstrates the request in which it searches for orders.
In addition to Bourne’s standard environment variables, the bash shell also provides several variables, as shown:
The bash Shell Bourne Variables:
CDPATH: A colon-separated list of directories used as a search path for the cd command.
HOME: The current user’s home directory.
IFS: A list of characters that separate fields used by the shell to split text strings.
MAIL: The filename for the current user’s mailbox. The bash shell checks this file for new mail.
MAILPATH: A colon-separated list of multiple filenames for the current user’s mailbox. The bash shell checks each file in this list for new mail.
OPTARG: The value of the last option argument processed by the getopts command.
OPTIND: The index value of the last option argument processed by the getopts command.
PATH: A colon-separated list of directories where the shell looks for commands.
PS1: The primary shell command line interface prompt string.
PS2: The secondary shell command line interface prompt string.
The bash Shell Environment Variables:
BASH: The full pathname to execute the current instance of the bash shell.
BASH_ENV: When set, each bash script attempts to execute a startup file defined by this variable before running.
BASH_VERSION: The version number of the current instance of the bash shell.
BASH_VERSINFO: A variable array that contains the individual major and minor version numbers of the current instance of the bash shell.
COLUMNS: Contains the terminal width of the terminal used for the current instance of the bash shell.
COMP_CWORD: An index into the variable COMP_WORDS, which contains the current cursor position.
COMP_LINE: The current command line.
COMP_POINT: The index of the current cursor position relative to the beginning of the current command.
COMP_WORDS: A variable array that contains the individual words on the current command line.
COMPREPLY: A variable array that contains the possible completion codes generated by a shell function.
DIRSTACK: A variable array that contains the current contents of the directory stack.
EUID: The effective user ID of the current user.
FCEDIT: The default editor used by the fc command.
FIGNORE: A colon-separated list of suffixes to ignore when performing filename completion.
FUNCNAME: The name of the currently executing shell function.
GLOBIGNORE: A list of colon-separated patterns, defining the set of filenames to be ignored by expanding the filename.
GROUPS: A variable array containing the list of groups of which the current user is a member.
histchars: Up to three characters which control history expansion.
HISTCMD: The history number of the current command.
HISTCONTROL: Controls what commands are entered in the shell history list.
HISTFILE: The name of the file to save the shell history list (.bash history by default).
HISTFILESIZE: The maximum number of lines to save in the history file.
HISTIGNORE: A list of pattern separated by a colon used to decide which commands are being ignored for a history file.
HISTSIZE: The maximum number of commands stored in the history file.
HOSTFILE: Contains the name of the file that should be read when the shell needs to complete a hostname.
HOSTNAME: The name of the current host.
HOSTTYPE: A string that describes the bash-shell machine works.
IGNOREEOF: The number of consecutive EOF characters the shell must receive before exiting. If this value doesn’t exist, the default is one.
INPUTRC: The name of the Readline initialization file (the default is .inputrc).
LANG: The locale category for the shell.
LC_ALL: Overrides the LANG variable, defining a locale category.
LC_COLLATE: Sets the collation order used when sorting string values.
LC_CTYPE: Determines the interpretation of characters used in filename expansion and pattern matching.
LC_MESSAGES: Determines the locale setting used when interpreting double-quoted strings preceded by a dollar sign.
LC_NUMERIC: Specifies the local setting used for number formatting.
LINENO: The line number in a script currently executing.
LINES: Defines the number of lines available on the terminal.
MACHTYPE: A string defining the system type in cpu-company-system format
MAILCHECK: How often the shell should check (default is 60 seconds) for new mail.
OLDPWD: The previous working directory used in the shell.
OPTERR: If set to 1, the bash shell displays errors generated by the getopts command.
OSTYPE: A string defining the operating system the shell is running on.
PIPESTATUS: A variable array containing a list of exit status values from the processes in the foreground process.
POSIXLY_CORRECT: If set, bash starts in POSIX mode.
PPID: The process ID (PID) of the bash shell’s parent process.
PROMPT_COMMAND: If set, the command to execute before displaying the primary prompt.
PS3: The prompt to use the select command.
PS4: The prompt displayed before the command line is echoed if the bash -x parameter is used.
PWD: The current working directory.
RANDOM: Returns a random number between 0 and 32767. Assigning a value to this variable seeds the random number generator.
REPLY: The default variable for the read command.
SECONDS: The number of seconds since the shell was started. Assigning a value resets the timer to the value.
SHELLOPTS: A colon-separated list of enabled bash shell options.
SHLVL: Indicates the shell level, incremented by one each time a new bash shell is started.
TIMEFORMAT: A format specifying how the shell displays time values.
TMOUT: The value of how long (in seconds) the select and read commands should wait for input. The default of zero indicates to wait indefinitely.
UID: The real user id of the current user.
You may notice that not all of the default environment variables are shown after we used the set command. The explanation for this can be that though these are the default environment variables, not all of them are required to contain a value.
In this guide, we examined the world of Linux environment variables. Global environment variables are often accessed from any child process spawned by the process they’re defined in. Local environment variables will only be accessed from the process within which they’re defined.
The Linux system uses each global and local environment variables to store info about the system environment. You’ll be able to access this info from the shell command line interface, as well as inside shell scripts. The bash shell uses the system environment variables defined within the original UNIX Bourne shell, also as a lot of new environment variables. The PATH environment variable defines the search pattern the bash shell takes to search out an executable command. You’ll be able to modify the PATH environment variable to add your own directories, or maybe the current directory symbol, to make running your programs easier. You can also create your own global and local environment variables for your own use. Once you create an environment variable, it’s accessible for the whole duration of your shell session.
There are many startup files that the bash shell executes once it starts up. This startup files can contain environment variable definitions to set standard environment variables for every bash session. Once you log in to the Linux system, the bash shell accesses the /etc/profile startup file, and also three local startup files for every user, $HOME/.bash profile, $HOME/.bash login, and $HOME/.profile.
Users will customize these files to include environment variables and startup scripts for their own use. The bash shell additionally provides for environment variable arrays. These environment variables can contain multiple values in a single variable. You’ll be able to access the values either individually by referencing an index value or as an entire by referencing the complete environment variable array name. Finally, in this guide, we need to mention the use of command aliases. While not environment variables, command aliases behave almost like environment variables. They permit you to define an alias name for a command, together with its parameters. Rather than having to type in a very long command and parameters, you will be able to simply assign it to an easy alias and use the alias at any time in your shell session.
Hopefully, by following these guide you successfully learn all about Linux environment variables. Of course, you don’t need to setting or removing Linux environment variables yourself if you use one of our Managed Linux VPS services, in which case you can simply ask our expert Linux admins to do it for you. They’re available 24/7 and will help you fix your broken packages immediately.
PS. If you liked this post on Guide to Linux Environment Variables, please share it with your friends on the social networks using the buttons below, or simply leave a comment. Thanks.