How to set the PATH of the environment variable in Ubuntu

Source: Internet
Author: User

In Ubuntu, there are two ways to set the PATH environment variable. The first option is to set the PATH for a single user, and the second option is to set the PATH for the global user.
Method 1:
There is a. bashrc file in the user's main directory. You can add the PATH settings in this file as follows:
Export PATH = "$ PATH:/your path1/:/your path2 /....."
Note: Each path must be separated.
Log out and restart X.
Method 2:
Add it to/etc/profile.
PATH = "$ PATH:/home/zhengb66/bin"
Export PATH

Environment variables are closely related to Shell. A Shell is started after the user logs on to the system. For Linux, bash is generally used, but you can reset or switch to another Shell. For UNIX, it may be CShelll. Environment variables are set through Shell commands. The configured environment variables can be used by all programs run by the current user. For the bash Shell program, the corresponding environment variables can be accessed through the variable name, and the environment variables can be set through export. The following describes several instances.

1) etc/profile:
This file sets the environment information for each user in the system. When the user logs on for the first time, this file is executed.
And collect shell settings from the configuration file in the/etc/profile. d directory.
Note: here we set global variables that can be used by all users.

2)/etc/bashrc:Execute this file for every user running bash shell. When bash shell is opened, the file is read.

3 )~ /. Bash_profile:Each user can use this file to input the shell information dedicated to his/her use,When a user logs on, this file is only executed once!By default, it sets some environment variables to execute the user's. bashrc file.
Note :~ In LINUX, the variable HOME is represented.
In addition, in different LINUX operating systems, this file may be different.
~ /. Bash_profile ;~ /. Bash_login or ~ /. One or more of the profiles. If there are several, the execution sequence is :~ /. Bash_profile ,~ /. Bash_login ,~ /. Profile. For example, if I use Ubuntu, only ~ is used by default in my user folder ~ /. Profile file.

4 )~ /. Bashrc:This file contains bash information dedicated to your bash shell. When you log on and open a new shell, the file is read.
(Note: This file starts with., so it is hidden in the folder)
So how do we add our own defined environment variables?
Open the file in notepad, and then write:
Xiaokang = kangkang
Save the variable so that each time a new terminal is opened, this variable takes effect. Remember, if you have opened a terminal and modified the file, it will not take effect under this terminal.
In general, you 'd better modify it here, but sometimes it will overwrite the parent-level variables. For example, if PATH is set as ROOT, but if you write PATH = xx in this file, in the future, all the paths will become xx, so we should write the following in this file:
PATH = $ PATH: xx
In this way, we can add the original and your own. In addition, in LINUX systems, use: Split instead of windo;
Both 3 and 4 are in the user directory. The only difference is that. bash_profile can only be started once at login. This 3 file does not seem to exist in my Ubuntu.

5 )~ /. Bash_logout:Execute this file every time you exit the system (exit bash shell.
In addition, the variables (global) set in/etc/profile can act on any user ~ The variables (local) set in/. bashrc can only inherit the variables in/etc/profile. They are \ "Parent-Child \" relationships.

~ /. Bash_profile is interactive and login to run in bash.
~ /. Bashrc runs in bash in interactive non-login mode.
Generally, the two settings are roughly the same, so the former usually calls the latter.

Okay. Let's summarize their execution methods:
When you log on to and log on to shell as bash, bash first executes the command in the/etc/profile file (if the file exists), and then searches ~ /. Bash_profile ,~ /. Bash_login or ~ /. Profile file, and execute the command in the first readable file found. When you log on to bash and exit, it will execute ~ /. Bash_logout file.
When an interactive bash is started, it will execute ~ /. Commands in the bashrc file (if the file exists and is readable). When a shell script is started non-interactively, bash searches for the bash_env environment variable and determines the name of the execution file.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.