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

Example Analysis of Environment variable configuration in Linux

2025-01-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

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

This article will explain in detail the example analysis of the configuration of environment variables in Linux. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.

Document

I have always hated the act of giving conclusions without provenance, which gives people a sense of "why should I trust you?" And in fact, the conclusion of random discussion without provenance is basically followed by others. In fact, instead of asking someone else, ask the document. After searching for a while, I found that the relevant documentation on the configuration of environment variables is actually in the man document of the bash command. After all, this is the shell that we often use.

In $man bash, I found the following paragraph:

INVOCATION A login shell is one whose first character of argument zero is a -, or one started with the-- login option. An interactive shell is one started without non-option arguments and without the-c option whose standard input and error are both connected to terminals (as determined by isatty (3)), or one started with the-I option. PS1 is set and $- includes i if bash is interactive, allowing a shell script or a startup file to test this state. The following paragraphs describe how bash executes its startup files. If any of the files exist but cannot be read, bash reports an error. Tildes are expanded in filenames as described below under Tilde Expan- sion in the EXPANSION section. When bash is invoked as an interactive login shell, or as a non-inter- active shell with the-login option, it first reads and executes com- mands from the file / etc/profile, if that file exists. After reading that file, it looks for ~ / .bash_profile, ~ / .bash_login, and ~ / .profile, in that order, and reads and executes commands from the first one that exists and is readable. The-- noprofile option may be used when the shell is started to inhibit this behavior. When a login shell exits, bash reads and executes commands from the file ~ / bash_logout, if it exists. When an interactive shell that is not a login shell is started, bash reads and executes commands from / etc/bash.bashrc and ~ / .bashrc, if these files exist. This may be inhibited by using the-- norc option. The-rcfile file option will force bash to read and execute commands from file instead of / etc/bash.bashrc and ~ /. Bashrc. When bash is started non-interactively, to run a shell script, for example, it looks for the variable BASH_ENV in the environment, expands its value if it appears there, and uses the expanded value as the name of a file to read and execute. Bash behaves as if the following com- mand were executed: if [- n "$BASH_ENV"]; then. "$BASH_ENV"; fi but the value of the PATH variable is not used to search for the file- name.

Through this passage, we find that the so-called environment variable configuration files are those files that are loaded automatically when shell logs in. But there are two types of landings that he defines:

Log in to login shell.

Log in to interactive shell.

Login shell login

The so-called login shell login actually refers to the login that requires the input of a password. Specifically, it includes boot login, ssh login, or typing bash-login, which is a way of "pretending to log in your password". In this login mode, the system will first read the / etc/profile file, and then the system will search for ~ / .bash _ profile, ~ / .bash_login, ~ / .profile, and run the file that only the first one exists. Pay particular attention to the "logical OR" relationship of the last three files. In many cases, we will find that the configuration does not take effect after re-logging in when the ~ / .profile file has been modified. This is because our system may have one of the first two files, so that we will not continue to read the remaining files.

The following three pictures illustrate this problem very well:

Interactive shell login

The so-called interactive shell login is actually relative to login shell login. We usually right-click to open the terminal after login, or CTRL+ALT+T to open the terminal is interactive shell login. In this login mode, the system will read / etc/bash.bashrc and ~ / .bashrc in turn, and execute them. Usually, some constants and aliases are recorded by default in the ~ / .bashrc file, especially the $PS1 variable, which determines the format, style, color, and so on of the bash prompt.

Note:

It should be noted that the two login methods read different configuration files and do not intersect each other, so when we need to configure environment variables, we have to configure the required variables to different files according to our own login method. For example, the following classic question.

Typical problems

An example of an abnormal configuration of the environment profile is when I log in to the server with ssh and find that the prompt looks like this:

Bash-4.3 $

Yes, just like the bash in the third picture above, the prompt is very strange, and there is no distinction between the color of the file and the folder when typing ls. This problem is obviously due to the fact that the environment variable $PS1 is not configured, which causes him to use the default value, although when looking at the .bashrc file, he finds the definition of the variable $PS1. But because ssh belongs to login shell, the configuration file he reads when logging in is a file such as / etc/profile, not .bashrc. The reason for this problem is usually that we mistakenly deleted the default configuration file in / etc/profile, so the solution is also very simple. Just copy some files from .bashrc to / etc/profile.

The lesson of this problem is that when we configure variables for the server, we should try to configure them in / etc/profile or ~ / .bash_profile, because .bashrc files are basically not needed to log in to the server with ssh. When we configure environment variables on our computers, try to configure them in .bashrc, so that we can just open the terminal and read this file, so that we can apply the configuration without logging out (configuration files such as / etc/profile will be applied only if we log out and log back in).

This is the end of the article on "sample analysis of the configuration of environment variables in Linux". I hope the above content can be helpful to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.

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

Servers

Wechat

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

12
Report