运行 su 时未获取 .bash_profile

Nul*_*ser 46 rhel su environment-variables

例如,我有一个用户user1对其 进行了修改.bash_profile,其中一个更改了PATH,例如:export PATH=/some/place:$PATH。如果我user1su - user1.

但是,如果我尝试通过suas运行命令root,例如:

su -c test.sh oracle
Run Code Online (Sandbox Code Playgroud)

(测试包含echo $PATH

它似乎没有修改PATH(或 root 的PATH,就此而言)。我也试过复制.bash_profile.profile,无济于事。

为什么会这样?

Kev*_*vin 72

使用su没有-l-开始bash作为一个互动的,但非登录shell,不从任何你指定的文件中读取。使用-lor-选项或将相关配置放入/root/.bashrc.

配置文件的快速摘要:

  • 登录壳(-l/ --login)读/etc/profile第一,然后它最先找到的:~/.bash_profile~/.bash_login,和~/.profile
  • 交互式但非登录的 shell ( -i) 读取/etc/bash.bashrc~/.bashrc, 以该顺序(除非使用该--rcfile选项并告诉它查看其他地方)。
  • 非交互式 shell,例如从另一个程序中启动而不使用-l-i标志,读取BASH_ENV环境变量中指定的文件。
  • 当作为sh登录 shell运行时,它将按顺序读取/etc/profile~/.profile
  • 当作为sh交互式非登录运行时,它会读取ENV.


Kyl*_*nes 5

Bash 的行为取决于它是否认为它是登录 shell,即当您登录到系统时运行的第一个 shell。它仅.bash_profile在它是登录外壳时读取。如果您将PATH更改代码放入其中.bashrc,它将为所有交互式 bash shell 运行,而不仅仅是登录 shell。