Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the environment variable configuration of Linux

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

Linux environment variable configuration is what, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain for you in detail, people with this need can come to learn, I hope you can gain something.

Linux environment variable configuration

When customizing the installation of software, you often need to configure environment variables. Here are various ways to configure environment variables.

The environment for all of the following examples is as follows:

System: Ubuntu 14.0 user name: uusama needs to configure MySQL environment variable path: / home/uusama/mysql/binLinux read environment variable

The method of reading environment variables:

The export command displays all the current system-defined environment variables echo $PATH command outputs the value of the current PATH environment variable

The effect of these two commands is as follows

Uusama@ubuntu:~$ export

Declare-x HOME= "/ home/uusama"

Declare-x LANG= "en_US.UTF-8"

Declare-x LANGUAGE= "en_US:"

Declare-x LESSCLOSE= "/ usr/bin/lesspipe 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 lookup path to run the command with a colon: split different paths, with or without double quotation marks when using the export definition.

Linux environment variable configuration method 1: export PATH

Use the export command to directly modify the value of PATH and configure the method for MySQL to enter the environment variable:

Export PATH=/home/uusama/mysql/bin:$PATH

# or put PATH in front

Export PATH=$PATH:/home/uusama/mysql/bin

Note:

Effective time: effective period immediately: the current terminal is valid, but the effective range is invalid after the window is closed: do not forget to add the original configuration, that is, the $PATH part, to the environment variables that are effectively configured by the current user, to avoid overwriting the original configuration Linux environment variable configuration method 2: vim ~ /. Bashrc

Configure by modifying the ~ / .bashrc file in the user directory:

Vim / .bashrc

# add on the last line

Export PATH=$PATH:/home/uusama/mysql/bin

Note:

Effective time: effective when a new terminal is opened with the same user, or manual source ~ / .bashrc effective period: permanent effective range: valid only for current users. If a subsequent environment variable loading file overrides the PATH definition, then Linux environment variable configuration method 3: vim ~ / .bashrc may not take effect.

Similar to modifying the ~ / .bashrc file, you need to add a new path to the end of the file:

Vim / .bash_profile

# add on the last line

Export PATH=$PATH:/home/uusama/mysql/bin

Note:

Effective time: effective when a new terminal is opened with the same user, or manual source ~ / .bash_profile effective period: permanent effective range: valid only for current users. If there is no ~ / .profile file, you can edit the ~ / .profile file or create a new Linux environment variable configuration method 4: vim / etc/bashrc

The method is to modify the system configuration, requiring administrator privileges (such as root) or write permissions to the file:

# if the / etc/bashrc file is not editable, it needs to be modified to editable

Chmod-v USBW / etc/bashrc

Vim / etc/bashrc

# add on the last line

Export PATH=$PATH:/home/uusama/mysql/bin

Note:

Effective time: effective time of newly opened terminal or manual source / etc/bashrc effective time limit: permanent effective range: effective Linux environment variable configuration method for all users 5: vim / etc/profile

This method modifies the system configuration and requires administrator permissions or write permissions to the file, similar to vim / etc/bashrc:

# if the / etc/profile file is not editable, it needs to be modified to editable

Chmod-v USBW / etc/profile

Vim / etc/profile

# add on the last line

Export PATH=$PATH:/home/uusama/mysql/bin

Note:

Effective time: effective time of newly opened terminal or manual source / etc/profile effective time limit: permanent effective range: effective Linux environment variable configuration method for all users 6: vim / etc/environment

The method is to modify the system environment configuration file, which requires administrator privileges or write permissions to the file:

# if the / etc/bashrc file is not editable, it needs to be modified to editable

Chmod-v USBW / etc/environment

Vim / etc/profile

# add on the last line

Export PATH=$PATH:/home/uusama/mysql/bin

Note:

Effective time: effective time of newly opened terminal or manual source / etc/environment effective time limit: permanent effective range: effective Linux environment variable loading principle analysis for all users

The various configuration methods for environment variables are listed above, so how does Linux load these configurations? In what order is it loaded?

A specific loading order can cause the definition of an environment variable with the same name to be overwritten or ineffective.

Classification of environmental variables

Environment variables can be simply divided into user-defined environment variables and system-level environment variables.

User-level environment variable definition files: ~ / .bashrc, ~ / .profile (some systems are: ~ / .profile) system-level environment variable definition files: / etc/bashrc, / etc/profile (some systems are: / etc/bash_profile), / etc/environment

In addition, in the user environment variables, the system will first read ~ / .bash_profile (or ~ / .profile) files, if there is no such file, read ~ / .bashrc, and then read ~ / .bashrc according to the contents of these files.

A method for testing the loading order of Linux environment variables

To test the loading order of environment variables for different files, we define the same environment variable UU_ORDER in the first line of each environment variable definition file, whose value is its own value concatenated with the current file name.

The files that need to be modified are as follows:

/ etc/environment/etc/profile/etc/profile.d/test.sh, create a new file, no folder to skip / etc/bashrc, or / etc/bash.bashrc/.bash_profile, or / .profile ~ / .bashrc

Add the following code to the first line of each file, and modify the colon to the absolute file name of the current file accordingly.

Export UU_ORDER= "$UU_ORDER:~/.bash_profile"

Save after modification, open a new window, and then 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

It can be inferred that the order in which Linux loads environment variables is as follows:

/ etc/environment/etc/profile/etc/bash.bashrc/etc/profile.d/test.sh~/.profile~/.bashrcLinux environment variable file loading detailed explanation

From the above tests, it is easy to conclude that the order in which Linux loads environment variables is as follows:

System environment variables-> user-defined environment variables

/ etc/environment-> / etc/profile-> ~ / .profile

Open the / etc/profile file and you will find that the / etc/bash.bashrc file is loaded in the code of the file, then check the .sh file in 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

Fi

Fi

If [- d / etc/profile.d]; then

For i in / etc/profile.d/*.sh; do

If [- r $I]; then

. $I

Fi

Done

Unset i

Fi

Secondly, open the ~ / .profile file, and you will find that the ~ / .bashrc file is loaded in the file.

# if running bash

If [- n "$BASH_VERSION"]; then

# include .bashrc if it exists

If [- f "$HOME/.bashrc"]; then

. "$HOME/.bashrc"

Fi

Fi

# set PATH so it includes user's private bin directories

PATH= "$HOME/bin:$HOME/.local/bin:$PATH"

It is not difficult to find from the code in the ~ / .profile file that the / .profile file is read only once when the user logs in, while / .bashrc is read every time the Shell script is run.

A few tricks

You can customize an environment variable file, such as defining uusama.profile under a project, using export to define a series of variables in this file, and then adding: sourc uusama.profile to the ~ / .profile file, so that you can use a series of variables defined by yourself in the Shell script every time you log in.

You can also use the alias command to define aliases for some commands, such as alias rm= "rm-I" (double quotes must be), and add this code to ~ / .profile, so that every time you use the rm command, it is equivalent to using the rm-I command, which is very convenient.

What is Linux system Linux is a free-to-use and free-spread UNIX-like operating system, is a POSIX-based multi-user, multi-task, multi-threaded and multi-CPU operating system, using Linux can run major Unix tools, applications and network protocols.

Is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report