Linux variables, environment variables, the role of export source, login shell and non-login shell

  • export
    • Export to make variables into environment variables, because ordinary variables cannot cross processes, so export is usually used to make variables visible between processes; there is another common example, expanding the PATH variable
[root@localhost ~]# myname=myvalue //普通变量
[root@localhost ~]# echo $myname
myvalue
[root@localhost ~]# sh //sh开启了新的shell,也就是开了新进程,
sh-4.4# echo $myname //myname变量在子进程中不可见,所以没有值

 [root@localhost ~]# export myname  //export为环境变量后
[root@localhost ~]# sh
sh-4.4# echo $myname//子进程中可见了
myvalue

export PATH=$PATH:/usr/local/openresty/nginx/sbin
  • source
    sourceor . (英文点号)in the current shell environment, read and execute the commands in the file. If there are environment variables in the script, they are usually derived as environment variables and take effect immediately.

  • login shell和non-login shell

    • The login shell is a shell that needs to enter a user name and password to use;
    • A non-login shell is a shell that does not need to enter a user name or password; for example, after logging in, the shell started is a non-login shell; after logging in, the shell opened by using the sh command is a non-login shell.

In addition to this difference, the biggest difference between them is that the environment configuration files they read are different. The login shell will read /etc/profile, ~/.bash_profile, and non-login shells only read~/.bashrc .
insert image description here

Guess you like

Origin blog.csdn.net/wangjun5159/article/details/131120565