[linux] Analysis of local variables, environment variables and global variables


1. Local variables

  • Local variables : current user-defined variables. Valid in the current process, invalid in other processes and child processes of the current process.

2. Environment variables

1. Classification of environment variables

  • Environment variable (bound to the program, when the program ends, the dynamically set environment variable will also end): the current process is valid and can be called by the child process, otherwise, the setting of the environment variable of the child process is invalid for the parent process.
    • 1. The value of the environment variable is determined by the user, the same as the user environment variable of Windows, even if the user qpyue uses su to switch to the root user, echo USER is still the previous user name (qpyue), and the value of the environment variable remains unchanged. (except HOME)

    • 2. Even if two qpyue users log in at the same time, their environment variables will be refreshed from the configuration file and stored separately, so the values ​​of the environment variables will not affect each other (during the existence of two bash programs, the environment changes of either one will not affect the other )

1) Classified according to the effective scope

System environment variables : public, valid for all users.
User environment variables : user-private and customized personalized settings, which only take effect for this user.

2) Classification by life cycle

Permanent environment variable : Configured in the environment variable script file, these scripts will be automatically executed every time the user logs in, which is equivalent to permanent effect.
Temporary environment variable : temporarily defined in the shell when used, and becomes invalid after exiting the shell.

2. Environment variable related commands

  • env: View the environment variables of the current user
  • env|grep environment variable name: filter included environment variables
  • export : Display all environment variables defined by the current system
  • export variable name = variable value or variable name = variable value; export variable name
  • printenv environment variable name: view the value of the specified environment variable
  • echo $environment variable name: view the value of the specified environment variable
  • unset environment variable name: view the value of the specified environment variable
  • set queries all variables of the current user (temporary variables and environment variables)

3. Common environment variables

PATH

Function and role : the search directory for executable programs, which include Linux system commands and user applications. If the directory of the executable program is not in the directory specified by PATH, you need to specify the directory when executing it.

Writing format : The PATH environment variable stores a list of directories, and the directories are separated by colons:, and the last dot. indicates the current directory. export PATH=directory1:directory2:directory3:...directoryn:.

add format

1、临时添加,PATH 在终端关闭后就会消失。
export PATH=$PATH:$HOME/bin 或 export PATH=$HOME/bin:$PATH
2、永久添加环境变量(影响当前用户)
vim ~/.bashrc

At the end of the document, add:

export PATH="/apps/summary_fz_province/cdc_model/bin/black_table:$PATH"

Save, exit, and run:

source /etc/profile
3、永久添加环境变量(影响所有用户)
vim /etc/profile

At the end of the document, add:

export PATH="/opt/STM/STLinux-2.3/devkit/sh4/bin:$PATH"

Save, exit, and run:

source /etc/profile

other environment variables

  • HOME: Specify the user's main working directory (that is, the default directory when the user logs in to the Linux system).
  • PWD: Display the current directory.
  • SHELL: Which Shell the current user is using.
  • HISTSIZE: Save the number of historical command records.
  • HOSTNAME: The name of the host. If the application needs to use the host name, it is usually obtained from this environment variable.
  • LOGNAME: The current user's login name.
  • LANG/LANGUGE: Language-related environment variables, users who use multiple languages ​​can modify this environment variable.

3. Global variables

  • Global variables: All users and programs can be called globally (file configuration), and inherited, and new users can also be called by default.
  • Related configuration files
file name illustrate Remark
/etc/profile Global environment variable information System and all users are in effect
/etc/bashrc Global bash information, valid for all users
$HOME/.bashrc The bash information of the current user, read when the user logs in Define aliases, umasks, functions, etc.
$HOME/.bash_profile The current user's environment information, read when the user logs in
$HOME/.bash_logout Last read when the current user exits the current shell Defines the program to be executed when the user logs out, etc.
$HOME/.bash_history Last read when the current user exits the current shell history -w save history history -c clear history

3.1 /etc/profile file

​ This file sets the environment information for each user of the system and is executed when the user logs in for the first time. And collect the shell's settings from the configuration files in the /etc/profile.d directory. If you have modified /etc/profile, you must manually execute source /etc/profile, and the modification will take effect . This modification will take effect for every user.

  • Execute etc/bash.bashrc if it exists.
  • If the /etc/profile.d directory exists, execute all script files ending with .sh in this directory.
  • Adding the PATH path can be added with the export command in the /etc/profile file.

3.2 /etc/profile.d file

​ /etc/profile will execute all script files under /etc/profile.d at each startup.

[wqf@b1i10 ~]$ ls /etc/profile.d 
256term.csh  abrt-console-notification.sh  colorgrep.csh  colorls.csh  csh.local   lang.csh  less.csh  PackageKit.sh  vim.csh  vte.sh      which2.sh
256term.sh   bash_completion.sh            colorgrep.sh   colorls.sh   flatpak.sh  lang.sh   less.sh   sh.local       vim.sh   which2.csh

3.2 /etc/bashrc file

​Execute this file for every user running Terminal. This file is read when the terminal is opened. If you want to modify a certain configuration for all users who use the terminal and take effect in the terminals opened in the future, you can modify this file. You don’t need to restart to modify this file, just reopen a terminal to take effect.

3.3 ~/.bash_profile file

​ Each user can use this file to enter the shell information dedicated to their own use. When the user logs in, the file is only executed once! By default, it sets some environment variables and executes the user's ~/.bashrc file. This file is similar to /etc/profile, and you need to manually execute source ~/.profile, and the modification will take effect . /etc/profile takes effect for all users, and ~/.profile only takes effect for the current user.

~/.bash_profile or ~/.bash_login or ~/.profile There is only one file, and the order of execution is: ~/.bash_profile > ~/.bash_login > ~/.profile

[summary_fz_province@b1i10 ~]$ cat ~/.bash_profile
# .bash_profile

# Get the aliases and functions
if [ -f ~/.bashrc ]; then
        . ~/.bashrc
fi

# User specific environment and startup programs

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

export PATH
---

3.4 ~/.bashrc file

This file is read when the user logs in and every time a new shell is opened. (Each user has a ~/.bashrc file in the user directory) This file is similar to /etc/bashrc, and it can take effect without restarting. Reopen a bash to take effect. /etc/bashrc is new for all users All opened bashes take effect, but ~/.bashrc only takes effect for the newly opened bash of the current user. It is not recommended to configure user-specific environment variables in it, because the file will be read once every time a Shell is opened, and the efficiency will definitely be affected.

3.5 The process of executing files when logging in to Linux

The process of executing a file when logging in to Linux is as follows:

1. When you first log in to Linux, first start the /etc/profile file, and then start one of the ~/.bash_profile, ~/.bash_login or ~/.profile files in the user directory.

2. Then start "~/.bash_profile" in the user directory, if "~/.bash_login" and "~/.profile" files exist. The order of execution is: ~/.bash_profile, ~/.bash_login, ~/.profile. If the ~/.bash_profile file exists, the ~/.bashrc file will generally be executed.

The execution sequence is : /etc/profile -> (~/.bash_profile | ~/.bash_login | ~/.profile) -> ~/.bashrc -> /etc/bashrc -> ~/.bash_logout

3.6 Differences between /etc/profile, /etc/bashrc, ~/.bash_profile and ~/.bashrc

/etc/profile is a global function, the variables set in it act on all users, and the variables set in ~/.bash_profile can inherit the variables in /etc/profile and act on users.
~/.bash_profile is run in bash in interactive and login mode; ~/.bashrc is run in bash in interactive non-login mode.


Reference articles:
https://www.cnblogs.com/gmq-sh/p/6971588.html
https://blog.csdn.net/weixin_48896613/article/details/127170565
https://blog.csdn.net/mayue_web /article/details/97023615

Guess you like

Origin blog.csdn.net/sodaloveer/article/details/130380098