-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
118 additions
and
0 deletions.
There are no files selected for viewing
60 changes: 60 additions & 0 deletions
60
tiddlers/Draft of '登录Shell与非登录Shell的环境变量加载顺序' by Gezi-lzq.tid
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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. | ||
>>> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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. | ||
>>> |