Self-study Linux shell6.3-positioning System environment variables

Source: Internet
Author: User

Click Back to learn the path of the Linux command line and shell script

6.3-System environment variable persistence

When you log in to the Linux system to launch a bash shell, bash looks for commands in several files by default, which becomes the startup file;
The Boot file for bash check depends on how you start the bash shell, and the bash shell is launched in 3 ways:

    • Log in as the default login shell;
    • As an interactive shell for a non-login shell, enter bash on the command line;
    • Executes the shell in the shell script as a non-interactive shell that runs the script;
1 Landing Shell

When you log into the Linux system, the login shell will read the command from 5 different boot files:

    • /etc/profile: The default bash Shell's main boot file, each user login will execute this boot file;
    • $HOME/.bash_profile: User-specific startup files;
    • $HOME/.BASHRC: User-specific startup files;
    • $HOME/.bash_login: User-specific startup files;
    • $HOME/.profile: User-specific startup files;

1.1/etc/profile files are primarily logged into the Linux system, bash executes the commands in the/etc/profile/startup file
The/etc/peofile file for each release has different settings and commands

The above program contains a for loop to iterate over all files in the/ETC/PROFILE.D directory

1.2 $HOME directory of startup files, the general Linux distribution only use the following 4 of the files inside:

    • $HOME/.bash_profile: User-specific startup files;
    • $HOME/.BASHRC: User-specific startup files;
    • $HOME/.bash_login: User-specific startup files;
    • $HOME/.profile: User-specific startup files;

They are hidden files, located in the user's home directory, so each user can edit these files and add their own environment variables, which take effect every time a bash shell session is started.
For example, a personal test machine is only. Profile. BASHRC

2 Interactive Shell

If you tap the Bash command at the command line prompt, the shell you launch is called an interactive shell;
If Bash is launched as an interactive shell, it will not access the/etc/profile file, but will go to the user's home directory to check. BASHRC exists;
The. bashrc file has two functions;

    • View common BASHRC files in/etc directory;
    • Instead, it gives the user a place to customize their own command aliases and private script functions;
3 non-interactive shell

The system executes a shell script with a non-interactive shell, except that it has no command-line prompt.
When the shell launches a non-interactive shell process, it checks the environment variable to see the startup file to be executed, and if there is a specified file, the shell executes the command in that file, which usually includes the shell scripting variable settings.

Summarize:

1. According to the life cycle of the variables, the Linux variables can be divided into two categories:

    • permanent : The configuration file needs to be modified and the variable will be permanently active.
    • temporary : With the Export command declaration, the variable is invalidated when the shell is closed.

2. permanent environment variable, valid for all users
Modifying the /etc/profile file is also possible, but if the release version of the upgrade is also updated, the custom variables will disappear
It is recommended to modify the/ETC/PROFILE.D directory to create a file that ends in. SH and place the custom Global environment variable .

3. Permanent environment variable, valid for single user
modify the. bash_profile file in the user directory

4. Temporary environment variable, valid only for current shell (BASH)
Define the variable directly under the shell's command line using [export variable name = variable value].
This variable is valid only under the current shell (bash) or its child shell (bash),
When the shell is closed, the variable is invalidated, and when the new shell is opened there is no such variable, and it needs to be redefined.

Self-study Linux shell6.3-positioning System environment variables

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.