Skip to content

Commit

Permalink
使用太记桌面版同步
Browse files Browse the repository at this point in the history
  • Loading branch information
Gezi-lzq committed Mar 10, 2024
1 parent ebb3a33 commit f0b79b7
Show file tree
Hide file tree
Showing 2 changed files with 118 additions and 0 deletions.
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.
>>>
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.
>>>

0 comments on commit f0b79b7

Please sign in to comment.