How to modify the file

  
when setting the environment variable in Linux

Under the Linux system, the environment variable needs to be modified at the same time. Because the Linux environment variable is divided into system level and user level, the method of modifying the file is different. I will give you a detailed introduction to the method of modifying the Linux environment variables.

and Shell environment variables are closely related, after the user login system launched a Shell. For Linux it is generally bash, but you can also reset or switch to another shell. For UNIX, it might be CShelll. Environment variables are set by the shell command, and the set environment variables can be used by all programs running by the current user. For the bash shell program, you can access the corresponding environment variables by variable name, and set the environment variables through export. The following is illustrated by several examples.

First, system level:

1) etc/profile: This file sets the environment information for each user of the system. When the user logs in for the first time, the file is executed. And collect the shell settings from the configuration file in the /etc/profile.d directory.

Note: Here we set global variables that are available to all users.

2) /etc/bashrc: Execute this file for each user running the bash shell. This file is read when the bash shell is opened.

Second, the user level (these files are in the home directory):

1)~/.bash_profile: Each user can use this file to enter the shell information dedicated to their own use, when This file is only executed once when the user logs in! By default, he sets some environment variables and executes the user's .bashrc file.

Note: ~ Below LINUX is the representative of HOME variable.

In addition, under different Linux operating systems, this file may be different, it may be ~/.bash_profile; ~/.bash_login or ~/.profile one or several, if there are several Then, the order of execution is: ~/.bash_profile, ~/.bash_login, ~/.profile. For example, I use Ubuntu, the default is only the ~/.profile file in my user folder.

2)~/.bashrc: This file contains bash information specific to your bash shell, which is read when you log in and each time you open a new shell.

(Note: This file is . At the beginning, so it is hidden in the folder)

So how do we add our own defined environment variables?

Open this file with Notepad, and then write it in the end:

xiaokang=kangkang

Then save it, so every time you open a new terminal, we This variable will take effect. Remember, if you have already opened a terminal and then you modified the file, it will not work under this terminal. In general, users should modify it here, but sometimes override the parent variable. For example, PATH is set by ROOT, but if you write PATH=xx in this file, all future PATHs will become xx. So, we should write in this file:

PATH=$PATH:xx

This adds the original to your own. And pay attention to the use of LINUX system: split represents parallel, not windo;

3 and 4 are in the user directory, their only difference is: .bash_profile can only be started once at login . This 3 file does not seem to be in my Ubuntu.

3)~/.bash_logout: Execute this file each time you quit the system (exit the bash shell).

In addition, the variables (global) set in /etc/profile can be applied to any user, and the variables (local) set in ~/.bashrc can only inherit from /etc/profile. Variables, they are /“ father/child/” relationship.

~/.bash_profile is an interactive, login way to run into bash

~/.bashrc is an interactive non-login way to get into bash running

Usually both The settings are roughly the same, so usually the former will call the latter.

Through the above file introduction, we can understand what files need to be modified when we need to set environment variables. To be effective for all users, we need to set system-level environment variables. On the contrary, you need to modify the user-level files (preferably modify the .profile file, the reason has already been mentioned above).

In addition, System Home summarizes their implementation:

When you log in and the login shell is bash, bash first executes the commands in the /etc/profile file (if the file Exists), then it looks for ~ /.bash_profile, ~/.bash_login or ~/.profile files in order, and executes the commands found in the first readable file. When the login bash exits, it will execute the commands in the ~/.bash_logout file.

When launching an interactive bash, it executes the commands in the ~/.bashrc file (if the file exists and is readable). When launched non-interactively to run a shell script, bash will look up the bash_env environment variable and determine the name of the executable file.

The above is the way to set the environment variable to modify the file. If you need to reset the environment variable, then the file modification is indispensable. I hope the method described in this article will help you.

Copyright © Windows knowledge All Rights Reserved