我可以将想要为所有交互式、非登录 shell 运行的命令放在哪里?

我可以将想要为所有交互式、非登录 shell 运行的命令放在哪里?

通常,在 bash 中,答案是 ~/.bashrc 或 /etc/bash.bashrc。但不幸的是,Ubuntu 分别从 ~/.profile 和 /etc/profile 获取这些文件。那么,我应该在 Ubuntu 上将这些命令放在哪里呢?

也可以看看为什么非登录 shell 不会调用 /etc/profile?如果您不熟悉这些文件。

答案1

如果你打开man bash,你会发现在第 150 行某处:

When an interactive shell that is not a login shell  is  started,  bash
reads  and  executes  commands  from /etc/bash.bashrc and ~/.bashrc, if
these files exist.  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 /etc/bash.bashrc and ~/.bashrc.

因此,您可以放心使用~/.bashrc(或者/etc/bash.bashrc,但我不建议您使用此系统范围的文件)来实现您的目的。是的,在 Ubuntu 中。

答案2

看来 Ubuntu 上没有此类命令的标准位置(不修改 Ubuntu 上可能被视为标准的行为)。以下是我想到的解决方法:

将其添加到开头/etc/profile

IS_LOGIN_SHELL=1

然后,在/etc/bash.bashrc~/.bashrc测试这个变量:

if [ -n "${IS_LOGIN_SHELL-}" ]; then
   # Put your commands here
fi

希望变量名不会与其他任何内容冲突,并且这不会改变 Ubuntu 其他部分可能依赖的内容。

相关内容