diff --git "a/tiddlers/Draft of '\347\231\273\345\275\225Shell\344\270\216\351\235\236\347\231\273\345\275\225Shell\347\232\204\347\216\257\345\242\203\345\217\230\351\207\217\345\212\240\350\275\275\351\241\272\345\272\217' by Gezi-lzq.tid" "b/tiddlers/Draft of '\347\231\273\345\275\225Shell\344\270\216\351\235\236\347\231\273\345\275\225Shell\347\232\204\347\216\257\345\242\203\345\217\230\351\207\217\345\212\240\350\275\275\351\241\272\345\272\217' by Gezi-lzq.tid" new file mode 100644 index 0000000..b10ba80 --- /dev/null +++ "b/tiddlers/Draft of '\347\231\273\345\275\225Shell\344\270\216\351\235\236\347\231\273\345\275\225Shell\347\232\204\347\216\257\345\242\203\345\217\230\351\207\217\345\212\240\350\275\275\351\241\272\345\272\217' by Gezi-lzq.tid" @@ -0,0 +1,60 @@ +created: 20240310100913991 +creator: Gezi-lzq +draft.of: 登录Shell与非登录Shell的环境变量加载顺序 +draft.title: 登录Shell与非登录Shell的环境变量加载顺序 +modified: 20240310101914995 +modifier: Gezi-lzq +tags: 脚本tips +title: Draft of '登录Shell与非登录Shell的环境变量加载顺序' by Gezi-lzq +type: + +!! 场景: + +1. 用户登录系统时首次获得的Shell(登录Shell) +2. 打开新的终端或在已有的Shell中执行新的Shell命令(非登录Shell) +3. 通过Crontab或SSH/Ansible执行命令 + +!! 原理: + +登录Shell初始化脚本加载顺序: + +* `/etc/profile` +* `~/.bash_profile` +* `~/.bashrc` +* `/etc/bashrc` + +非登录Shell初始化脚本加载顺序: + +* `~/.bashrc` +* `/etc/bashrc` + +!!注意事项: + +当脚本或命令在Crontab、SSH/Ansible中执行时,应确保所需的环境变量已在非登录Shell的初始化脚本中设置。 + +应避免依赖于登录Shell特有的环境配置,因为这些配置在非登录Shell中可能不可用。 + +!! 引用参考 + +GNU Bash手册关于启动文件的章节:https://www.gnu.org/software/bash/manual/bash.html#Startup-Files +man bash 命令行手册页,特别是 "INVOCATION" 部分,详细描述了Bash Shell的启动过程和读取的文件。 + + +<<< + +Invoked as an interactive non-login shell +When an interactive shell that is not a login shell is started, Bash reads and executes commands from ~/.bashrc, if that file exists. 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 ~/.bashrc. + +So, typically, your ~/.bash_profile contains the line + +if [ -f ~/.bashrc ]; then . ~/.bashrc; fi +after (or before) any login-specific initializations. + +Invoked non-interactively +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 command were executed: + +if [ -n "$BASH_ENV" ]; then . "$BASH_ENV"; fi +but the value of the PATH variable is not used to search for the filename. + +As noted above, if a non-interactive shell is invoked with the --login option, Bash attempts to read and execute commands from the login shell startup files. +>>> diff --git "a/tiddlers/\347\231\273\345\275\225Shell\344\270\216\351\235\236\347\231\273\345\275\225Shell\347\232\204\347\216\257\345\242\203\345\217\230\351\207\217\345\212\240\350\275\275\351\241\272\345\272\217.tid" "b/tiddlers/\347\231\273\345\275\225Shell\344\270\216\351\235\236\347\231\273\345\275\225Shell\347\232\204\347\216\257\345\242\203\345\217\230\351\207\217\345\212\240\350\275\275\351\241\272\345\272\217.tid" new file mode 100644 index 0000000..5b31306 --- /dev/null +++ "b/tiddlers/\347\231\273\345\275\225Shell\344\270\216\351\235\236\347\231\273\345\275\225Shell\347\232\204\347\216\257\345\242\203\345\217\230\351\207\217\345\212\240\350\275\275\351\241\272\345\272\217.tid" @@ -0,0 +1,58 @@ +created: 20240310100913991 +creator: Gezi-lzq +modified: 20240310101352396 +modifier: Gezi-lzq +tags: 脚本tips +title: 登录Shell与非登录Shell的环境变量加载顺序 +type: + +!! 场景: + +1. 用户登录系统时首次获得的Shell(登录Shell) +2. 打开新的终端或在已有的Shell中执行新的Shell命令(非登录Shell) +3. 通过Crontab或SSH/Ansible执行命令 + +!! 原理: + +登录Shell初始化脚本加载顺序: + +* `/etc/profile` +* `~/.bash_profile` +* `~/.bashrc` +* `/etc/bashrc` + +非登录Shell初始化脚本加载顺序: + +* `~/.bashrc` +* `/etc/bashrc` + +!!注意事项: + +当脚本或命令在Crontab、SSH/Ansible中执行时,应确保所需的环境变量已在非登录Shell的初始化脚本中设置。 + +应避免依赖于登录Shell特有的环境配置,因为这些配置在非登录Shell中可能不可用。 + +!! 引用参考 + +GNU Bash手册关于启动文件的章节:https://www.gnu.org/software/bash/manual/bash.html#Startup-Files +man bash 命令行手册页,特别是 "INVOCATION" 部分,详细描述了Bash Shell的启动过程和读取的文件。 + + +<<< + +Invoked as an interactive non-login shell +When an interactive shell that is not a login shell is started, Bash reads and executes commands from ~/.bashrc, if that file exists. 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 ~/.bashrc. + +So, typically, your ~/.bash_profile contains the line + +if [ -f ~/.bashrc ]; then . ~/.bashrc; fi +after (or before) any login-specific initializations. + +Invoked non-interactively +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 command were executed: + +if [ -n "$BASH_ENV" ]; then . "$BASH_ENV"; fi +but the value of the PATH variable is not used to search for the filename. + +As noted above, if a non-interactive shell is invoked with the --login option, Bash attempts to read and execute commands from the login shell startup files. +>>>