environment variable settings under Linux
1, under the Windows system, many software installations need to configure environment variables, such as installing the JDK, if you do not configure environment variables, run the JAVAC command in a directory other than the software installation, you will report a similar error that the file cannot be found.
2. What are environment variables? To put it simply, it is to specify a directory, when running the software, the relevant program will follow the directory to find the relevant files. The most practical function of setting variables is that you don't have to copy some DLL files into the system directory, and path is a system variable that is a series of paths for the system to search for DLL files.
Under the Linux system, if you download and install the application, it is very likely that the "command not found" prompt will appear when you type its name. If you go to the installation directory folder every time, it is too cumbersome to find an executable file to work with. This involves setting the environment variable path, which is also a part of customizing the environment variable under Linux
three ways to set environment variables under Linux :
If you want to add a path to $path, you can do this as follows:
Environment variables that work only on the current shell
1. console Settings , not in favor of this way, because he only works on the current shell, and changing a shell setting is invalid:
Direct console input: $PATH = "$PATH":/new_path (Close shell Path will revert to the original path)
Environment variables that work for all users
2, modify the /etc/profile file, if your computer only as a development use of the push to use this method, because all the user's shell has the right to use this environment variable, may bring security issues to the system. This is for all the users, all the shells
Vi/etc/profile
Add at the bottom of/etc/profile: Export path= "$PATH:/new_path"
Environment variables that work for the current specific user
3, Modify the BASHRC file, this method is more secure, it can use these environment variable permissions to control the user level, here is for a specific user, if you need to give a user permissions to use these environment variables, you only need to modify their personal user home directory. The BASHRC file is available.
VI ~/.BASHRC
Add the following:
Export path= "$PATH:/new_path"
Ubuntu Linux System environment variable configuration file:
/etc/profile: The first file that is used when the operating system customizes the user environment at logon, which sets the environment information for each user of the system, which is executed when the user logs on for the first time.
/etc/environment: The second file used by the operating system at logon, the system sets environment variables for environment files before reading your own profile.
~/.profile: The third file that is used to log on is the. profile file, which each user can use to enter shell information that is specific to their own use, which is executed only once when the user logs on. By default, he sets some environment variables, Executes the user's. bashrc file.
/ETC/BASHRC: Executes this file for each user running the bash shell. When the bash shell is opened, the file is read.
~/.BASHRC: This file contains bash information dedicated to your bash shell, which is read when you log in and every time you open a new shell.
pash How to set environment variables:
Method One: Use the. Profile or. bashrc file in the home directory (recommended)
Log in to your user (non-root) and enter in the terminal:
$ sudo gedit ~/.profile (or. bashrc)
The following settings can be added to the path at the end of this file:
Export path= "$PATH: Your path1:your path2 ..."
Save the file, log off and log in, and the variable takes effect.
Variables added in this way are valid only for the current user.
Method Two: The profile file under the system directory (caution)
In the system's etc directory, there is a profile file that edits the file:
$ sudo gedit/etc/profile
The following settings are added to the path at the end:
Export path= "$PATH: Your path1:your path2 ..."
After the file edits are saved, the system restarts and the variables take effect.
The variables added in this way are valid for all users.
Method Three: Environment file under the system directory (caution)
In the system's etc directory, there is a environment file that edits the file:
$ sudo gedit/etc/environment
Locate the following PATH variable:
Path= "<......>"
Modify the path variable to include your own path, for example:
Path= "<......>:your path1:your path2 ..."
Each path is separated by a colon. The file is also restarted to take effect, affecting all users.
Note that this is not the add export path= ....
Method Four: Enter directly under the terminal
$ sudo export path= "$PATH: Your path1:your path2 ..."
This way the variable takes effect immediately, but the setting becomes invalid after the user logs off or the system restarts, and is appropriate for the temporary variable.
Note: Changes to methods two and three need to be cautious, especially if the root user modifies the error, which can cause some serious system errors. So I recommend using the first method. In addition, the development of embedded Linux is best not done under root (unless you are very familiar with Linux!!) ) to avoid serious system errors caused by improper operation.
The following is an example of a problem caused by error modification of a environment file and a workaround:
Problem: Unable to log on due to accidentally setting environment variable in etc/environment
Tip: Do not set export path in etc/environment this will cause the system to log on after a reboot
Workaround:
In the login interface alt +CTRL+F1 into command mode, if not the root user needs to type (root user not so verbose, gedit edit will not be displayed)
/usr/bin/sudo/usr/bin/vi/etc/environment
The cursor moves to the export path** line, and the row is deleted consecutively by D two times;
Input: Wq save exit;
Then type/sbin/reboot reboot system (may prompt need to boot, this time direct power off)
environment variable settings under Linux