This is a guide on environment variable configuration for the Linux operating system.
Environment Variable Configuration in Linux
We often need to configure environment variables when we customize the software installation. The following sections describe various configuration methods for environment variables.
The environment in the following examples is listed below:
- System: Ubuntu 14.0
- User Name: uusama
- Path of MySQL Environment Variables: /home/uusama/mysql/bin
Read Environment Variables in Linux
The methods to read environment variables are listed below:
- The export command displays all currently defined environment variables.
- The
echo $PATH
command outputs the value of current PATH environment variables.
The effects of these two commands are listed below:
uusama@ubuntu:~$ exportdeclare -x HOME="/home/uusama"declare -x LANG="en_US.UTF-8"declare -x LANGUAGE="en_US:"declare -x LESSCLOSE="/usr/bin/lesspipe %s %s"declare -x LESSOPEN="| /usr/bin/lesspipe %s"declare -x LOGNAME="uusama"declare -x MAIL="/var/mail/uusama"declare -x PATH="/home/uusama/bin:/home/uusama/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"declare -x SSH_TTY="/dev/pts/0"declare -x TERM="xterm"declare -x USER="uusama"uusama@ubuntu:~$ echo $PATH/home/uusama/bin:/home/uusama/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
The PATH variable defines the search path for running commands. Different paths are separated with colons (:). You can use the export definition with or without double quotation marks (").
Method 1 of Environment Variable Configuration in Linux: export PATH
Use the export
command to modify the value of PATH and configure the method of accessing the environment variables for MySQL:
export PATH=/home/uusama/mysql/bin:$PATH Or put PATH in frontexport PATH=$PATH:/home/uusama/mysql/bin
Note:
- Effective Time: It takes effect immediately.
- Effective Period: The current terminal is valid and will be invalid after the window is closed.
- Scope: It only applies to the current user.
Don't forget to add the original configuration ($PATH
) to the configured environment variable to avoid overwriting the original configuration.
Method 2 of Environment Variable Configuration in Linux: vim ~/.bashrc
Modify the ~/.bashrc file in user directory to configure:
vim ~/.bashrc# Add in the last lineexport PATH=$PATH:/home/uusama/mysql/bin
Note:
- Effective Time: The time when the same user opens a new terminal or when a user runs source ~/.bashrc manually
- Effective Period: Permanently valid
- Scope: It only applies to the current user.
If the PATH definition is overwritten when a subsequent environment variable loads the file, it may not take effect.
Method 3 of Environment Variable Configuration in Linux: vim ~/.bash_profile
This is similar to modifying the ~/.bashrc file; you only need to add a new path at the end of the file:
vim ~/.bash_profile# Add in the last lineexport PATH=$PATH:/home/uusama/mysql/bin
Note:
- Effective Time: The time when the same user opens a new terminal or when a user runs source ~/.bash_profile manually
- Effective Period: Permanently valid
- Scope: It only applies to the current user.
If the ~/.bash_profile file does not exist, edit the ~/.profile file or create a new one.
Method 4 of Environment Variable Configuration in Linux: vim /etc/bashrc
This method modifies the system configurations and requires administrator permissions (such as root) or writing permission for the file:
# If the /etc/bashrc file cannot be edited, it needs to be changed to the editable state.chmod -v u+w /etc/bashrcvim /etc/bashrc# Add in the last lineexport PATH=$PATH:/home/uusama/mysql/bin
Note:
- Effective Time: It takes effect when opening a new terminal or running source /etc/bashrc manually.
- Effective Period: Permanently valid
- Scope: It applies to all users.
Method 5 of Environment Variable Configuration in Linux: vim/etc/profile
This method modifies system configurations and requires administrator permissions or writing permission for the file. It is similar to vim/etc/bashrc:
# If the /etc/profile file cannot be edited, it needs to be changed to the editable state.chmod -v u+w /etc/profilevim /etc/profile# Add in the last lineexport PATH=$PATH:/home/uusama/mysql/bin
Note:
- Effective Time: It takes effect when opening a new terminal or running source /etc/profile manually.
- Effective Period: Permanently valid
- Scope: It applies to all users.
Method 6 of Environment Variable Configuration in Linux: vim/etc/environment
This method modifies system environment configuration files and requires administrator permissions or writing permission for the file:
# If the /etc/bashrc file cannot be edited, it needs to be changed to the editable state.chmod -v u+w /etc/environmentvim /etc/profile# Add in the last lineexport PATH=$PATH:/home/uusama/mysql/bin
Note:
- Effective Time: It takes effect when opening a new terminal or manually running source /etc/environment.
- Effective Period: Permanently valid
- Scope: It applies to all users.
Explanation of Environment Variables Loading in Linux
The sections above have described various configuration methods of environment variables, but how does Linux load these configurations? What is the loading order?
The particular loading order causes environment variable definitions with the same name to be overwritten or invalid.
Classification of Environment Variables
Environment variables can be divided into user-defined and system-level environment variables.
- Definition Files of User-Level Environment Variables: ~/.bashrc and ~/.profile (for some systems: ~/.bash_profile)
- Definition Files of System-Level Environment Variables: /etc/bashrc, /etc/profile (for some systems: /etc/bash_profile), and /etc/environment
In addition, the system reads the ~/.bash_profile (or ~/.profile) file first for user-level environment variables. If there is no such file, read ~/.bash_login and then read ~/.bashrc based on the content of these files.
Method of Testing the Environment Variable Loading Order in Linux
We define the same environment variable UU_ORDER in the first line of each environment variable definition file to test the loading order of environment variables in different files. The value of this variable is its own value plus the current file name.
The files that need to be modified are listed below:
/etc/environment/etc/profile/etc/profile.d/test.sh. Create a file. If no folder exists, skip this file./etc/bashrc or /etc/bash.bashrc~/.bash_profile or ~/.profile~/.bashrc
Add the following code to the first line of each file and change the content after the colon to the absolute file name of the current file accordingly.
export UU_ORDER="$UU_ORDER:~/.bash_profile"
Complete the changes and save them. Then, open a new window and run echo $UU_ORDER to observe the value of the variable:
uusama@ubuntu:~$ echo $UU_ORDER$UU_ORDER:/etc/environment:/etc/profile:/etc/bash.bashrc:/etc/profile.d/test.sh:~/.profile:~/.bashrc
The order of loading environment variables in Linux can be inferred like this:
/etc/environment/etc/profile/etc/bash.bashrc/etc/profile.d/test.sh~/.profile~/.bashrc
A Detailed Explanation of Environment Variable File Loading in Linux
From the tests above, it can be concluded that the order of Linux loading environment variables is:
System environment variables -> user-defined environment variables/etc/environment -> /etc/profile -> ~/.profile
Open the /etc/profile file, and you will find that the code of the file can load the /etc/bash.bashrc file. Then, check the .sh file under the /etc/profile.d/ directory and load it.
# /etc/profile: system-wide .profile file for the Bourne shell (sh(1))# and Bourne compatible shells (bash(1), ksh(1), ash(1), ...).if [ "$PS1" ]; then if [ "$BASH" ] && [ "$BASH" != "/bin/sh" ]; then # The file bash.bashrc already sets the default PS1. # PS1='\h:\w\$ ' if [ -f /etc/bash.bashrc ]; then . /etc/bash.bashrc fi else if [ "`id -u`" -eq 0 ]; then PS1='# ' else PS1='$ ' fi fifiif [ -d /etc/profile.d ]; then for i in /etc/profile.d/*.sh; do if [ -r $i ]; then . $i fi done unset ifi
Open the ~/.profile file again, and you will see that the ~/.bashrc file is loaded in this file.
# if running bashif [ -n "$BASH_VERSION" ]; then # include .bashrc if it exists if [ -f "$HOME/.bashrc" ]; then . "$HOME/.bashrc" fifi # set PATH so it includes user's private bin directoriesPATH="$HOME/bin:$HOME/.local/bin:$PATH"
According to the code of the ~/.profile file, the /.profile file only reads when the user logs in, while /.bashrc is read once every time the Shell script runs.
Tips
You can customize an environment variable file, such as defining uusama.profile under a project. Use export to define a series of variables in the file, and then add sourc uusama.profile after the ~/.profile file. By doing so, you can use a series of customized variables in the Shell script every time you log in.
You can also use the alias command to define the alias of some commands. For example, alias rm = "rm -i"
. (The double quotation marks are required.) Add the code to ~/.profile. It is convenient each time you run the rm
command because it is like running the rm -i
command.
This article was originally published on the Programmer Bai Nannan official WeChat account.
Disclaimer: The views expressed herein are for reference only and don't necessarily represent the official views of Alibaba Cloud.
FAQs
How do I set environment variables in Linux? ›
- Configure look and feel of shell.
- Setup terminal settings depending on which terminal you're using.
- Set the search path such as JAVA_HOME, and ORACLE_HOME.
- Create environment variables as needed by programs.
To set an environment variable, use the command " export varname=value ", which sets the variable and exports it to the global environment (available to other processes). Enclosed the value with double quotes if it contains spaces. To set a local variable, use the command " varname =value " (or " set varname =value ").
What are the environment variables in Linux? ›Here are some examples of environment variables in Linux: USER – This points to the currently logged-in user. HOME – This shows the home directory of the current user. SHELL – This stores the path of the current user's shell, such as bash or zsh.
What are 3 types of environment variables in Linux shell? ›Most Common Environment Variables
SHELL – Current shell (bash, zsh, etc.). LOGNAME – Name of the user. UID – User's unique identifier.
➢ In Linux (Shell), there are two types of variable: ➢ System variables: Created and maintained by Linux itself, this type of variable defined in CAPITAL LETTERS. ➢ User defined variables (UDV) : Created and maintained by user, this type of variable defined in lower letters.
Where environment variables are stored in Linux? ›bashrc file is one of the most commonly used files for storing environment variables. By default, starting a new terminal shell session executes the ~/. bashrc file. This file makes the perfect place to set environment variables that you want to be available in all future shell sessions.
Why do we need to set environment variables in Linux? ›The environment variables are dynamic values that are stored within a system and used by applications launched in shells or sub-shells. These variables have a name and their respected value. The environment variable customizes the system performance and the behavior of an application.
What is a environment command in Linux? ›The env command allows you to display your current environment or run a specified command in a changed environment. If no flags or parameters are specified, the env command displays your current environment, showing one Name=Value pair per line.
What are the types of environment variables? ›There are two types of environment variables: user environment variables (set for each user) and system environment variables (set for everyone). By default, a child process inherits the environment variables of its parent process.
What is environment variable example? ›Examples of environment variables include: PATH : a list of directory paths. When the user types a command without providing the full path, this list is checked to see whether it contains a path that leads to the command.
How are variables stored in Linux? ›
The Global environment variables of your system are stored in /etc/environment . Any changes here will get reflected throughout the system and will affect all users of the system.
How do I set an environment variable in shell? ›You can set your own variables at the command line per session, or make them permanent by placing them into the ~/. bashrc file, ~/. profile , or whichever startup file you use for your default shell. On the command line, enter your environment variable and its value as you did earlier when changing the PATH variable.
What is environment configuration? ›Environment configuration refers to identifying, modifying, and managing the interface dependencies required for the system to successfully progress from development to QA to production.
How do you set environment variables in Unix? ›- Set the CCM_HOME variable. For example: $ CCM_HOME=CCM_HOME;export CCM_HOME.
- To the beginning of the path, add CCM_HOME/bin and CCM_HOME/jre/bin . For example: ...
- Set the DISPLAY variable, unless you are installing in command-line mode. For example:
Environment variable names used by the utilities in the Shell and Utilities volume of IEEE Std 1003.1-2001 consist solely of uppercase letters, digits, and the '_' (underscore) from the characters defined in Portable Character Set and do not begin with a digit.
What are the 5 basic components of Linux? ›- Bootloader. Your computer needs to go through a startup sequence called booting. ...
- OS Kernel. ...
- Background services. ...
- OS Shell. ...
- Graphics server. ...
- Desktop environment. ...
- Applications.
In Linux there are basically three types of files: Ordinary/Regular files. Special files. Directories.
How do environment variables work? ›An environment variable is a variable whose value is set outside the program, typically through functionality built into the operating system or microservice. An environment variable is made up of a name/value pair, and any number may be created and available for reference at a point in time.
Which symbol is used with the environment variable name in Linux? ›Vector environment variables in Linux are active only when you are using Vector. A small subset of these environment variables can be reset in the local environment by the user. Most Vector environment variables are set in the Vector symbol table (symbol. tbl) and are visible only with the user command ingprenv.
How do I check environment variables? ›Select Start > All Programs > Accessories > Command Prompt. 2. In the command window that opens, enter echo %VARIABLE%. Replace VARIABLE with the name of the environment variable you set earlier.
What is the scope of environment variable in Linux? ›
Scope of any variable is the region from which it can be accessed or over which it is defined. An environment variable in Linux can have global or local scope. A globally scoped ENV that is defined in a terminal can be accessed from anywhere in that particular environment which exists in the terminal.
What is the variable used for in Linux? ›Simply put, environment variables are a set of dynamic named values stored within the system that is used by applications. These variables allow you to customize how specific applications and services behave with the system. Each variable contains a name and an associated value.
How do I find environment in Linux? ›- printenv command – Print all or part of environment.
- env command – Display all exported environment or run a program in a modified environment.
- set command – List the name and value of each shell variable.
What Is a UNIX Environment Variable? UNIX environment variables are variables that apply to both the current shell and to any subshells that it creates (for example, when you send a job to the background or execute a script).
What are the 3 main types of environment? ›The three types of environment are: Internal environment. External macro environment. External micro environment.
What are the 5 variables? ›- Categorical variables. A categorical variable (also called qualitative variable) refers to a characteristic that can't be quantifiable. ...
- Nominal variables. ...
- Ordinal variables. ...
- Numeric variables. ...
- Continuous variables. ...
- Discrete variables.
You can see that one way to look at variables is to divide them into four different categories ( nominal, ordinal, interval and ratio).
What are the 3 kinds of variables and their uses? ›There are three main variables: independent variable, dependent variable and controlled variables. Example: a car going down different surfaces. Independent variable: the surface of the slope rug, bubble wrap and wood. Dependent variable: the time it takes for the car to go down the slope.
Why is environment variable used? ›Environment variables help programs know what directory to install files in, where to store temporary files, and where to find user profile settings. They help shape the environment that the programs on your computer use to run.
What is variable simple answer? ›A variable is a quantity that may change within the context of a mathematical problem or experiment. Typically, we use a single letter to represent a variable. The letters x, y, and z are common generic symbols used for variables.
What is $_ in Linux? ›
$_ (dollar underscore) is another special bash parameter and used to reference the absolute file name of the shell or bash script which is being executed as specified in the argument list. This bash parameter is also used to hold the name of mail file while checking emails.
Does Linux have variables? ›Linux environment variables can be global or local. The global environment variables are visible from a shell session and any child processes that the shell spawns. While local variables can be available in the shell only in which they are created.
What is '$' in shell? ›Show activity on this post. $@ is basically use for refers all the command-line arguments of shell-script. $1 , $2 , $3 refer to the first command-line argument, the second command-line argument, third argument. Follow this answer to receive notifications.
How do you set a command as a variable? ›Syntax SET variable SET variable=string SET "variable=string" SET "variable=" SET /A "variable=expression" SET /P variable=[promptString] SET " Key variable : A new or existing environment variable name e.g. _num string : A text string to assign to the variable.
How do you set a variable in terminal? ›- Set the variable on its own line, then use it anywhere: $ SOMETHING="some value" $ echo $SOMETHING some value.
- Set the variable before a command, on the same line: $ SOMETHING="a value" env ... SOMETHING=a value ...
To set system-wide Linux environment variables, you can edit the /etc/environment file. Instead of appending export commands to the file, append the <NAME>='<VALUE>' pair to the end of the file.
How do I see environment variables in Linux? ›- printenv command – Print all or part of environment.
- env command – Display all exported environment or run a program in a modified environment.
- set command – List the name and value of each shell variable.
- put a file named 90java.conf below /etc/environment.d holding. JAVA_HOME=/usr/lib/jvm/default-java.
- put a file named java-env.sh below /etc/profile.d holding. export JAVA_HOME=/usr/lib/jvm/default-java.
- add the same text at the end to /etc/profile.
- find /usr/lib/jvm/java-1.x.x-openjdk.
- vim /etc/profile. Prepend sudo if logged in as not-privileged user, ie. sudo vim.
- Press 'i' to get in insert mode.
- add: export JAVA_HOME="path that you found" export PATH=$JAVA_HOME/bin:$PATH.
- Reboot your system, and voila.
There are two types of environment variables: user environment variables (set for each user) and system environment variables (set for everyone). By default, a child process inherits the environment variables of its parent process.
Where is JRE path in Linux? ›
Alternatively, you can use the whereis command and follow the symbolic links to find the Java path. The output tells you that Java is located in /usr/bin/java. Inspecting the directory shows that /usr/bin/java is only a symbolic link for /etc/alternatives/java.
Where is JDK path Linux? ›The JDK software is installed on your computer, for example, at /usr/jdk/jdk1. 6.0_02. You can change this location.
What is path and JAVA_HOME? ›JAVA_HOME is an Environment Variable set to the location of the Java directory on your computer. PATH is an internal DOS command that finds the /bin directory of the version of Java that you are using.