是否有所有 shell 读取的“.bashrc”等效文件?

Ste*_*fan 126 bash environment-variables profile

~/.bashrc 唯一指定用户特定环境变量、别名、PATH变量修改等的地方吗?

我问是因为这似乎~/.bashrcbash-only,但其他 shell 也存在......

msw*_*msw 103

该文件$HOME/.profile被许多 shell 使用,包括 bash、sh、dash 和其他可能的 shell。

从 bash 手册页:

当 bash 作为交互式登录 shell 被调用时,...它首先从文件 /etc/profile 中读取并执行命令,如果该文件存在的话。读取该文件后,它会按顺序查找 ~/.bash_profile、~/.bash_login 和 ~/.profile,然后从第一个存在且可读的命令中读取和执行命令。

csh 和 tcsh 明确不看,~/.profile但这些 shell 有点过时了。

  • tcsh 在某些环境中仍然很流行。 (10认同)
  • Zsh 默认不读取 .profile。这就是为什么我删除了我之前的回答说明这一点。Zsh 仅在被名为 sh 的符号链接调用时才读取 .profile。 (8认同)
  • 对于这种工作方式,用户需要确保每个 shell 都是一个登录 shell。例如,在 Gnome 终端中,转到 Profile -> Title and Command,并启用“Run command as a login shell”。您还需要删除`~/.bash_profile`,或使其成为源`~/.profile`。 (2认同)

Gil*_*il' 68

~/.profile是环境变量定义和您希望在登录时运行的非图形程序(例如ssh-agentscreen -m)的正确位置。如果您的登录 shell 是 Bourne 风格的 shell(sh、ksh、bash),它将由您的登录 shell 执行。Zsh 运行~/.zprofile,而 Csh 和 tcsh 运行~/.login

如果您在 X 显示管理器(xdm、gdm、kdm、...)下登录,是否~/.profile运行取决于您的显示管理器和桌面环境是如何由您的发行版配置的。如果您在“自定义会话”下登录,通常会执行~/.xsession.

~/.bashrc是 bash 特定设置的正确位置,例如别名、函数、shell 选项和提示。顾名思义,它特定于 bash;csh 有~/.cshrc,ksh 有~/.kshrc,zsh 有 <drumroll> ~/.zshrc

也可以看看:


Mik*_*kel 31

没有通用文件,但您可以让每个 shell 从一个通用文件中读取。

  1. bash.bash_profile或读取.bashrc
  2. zsh读取.zprofile .zshrc
  3. ksh.profile或读取$ENV

所以这就是我要做的:

~/.env

# Put environment variables here, e.g.
PATH=$PATH:$HOME/bin
Run Code Online (Sandbox Code Playgroud)

~/.shrc

test -f "$HOME/.env" && . "$HOME/.env"

# Put interactive shell setup here, e.g.
alias ll='ls -l'
PS1='$PWD$ '
set -o emacs
Run Code Online (Sandbox Code Playgroud)

~/.bashrc

test -f ~/.shrc && source ~/.shrc

# Put any bash-specific settings here, e.g.
HISTFILE=~/.bash_history
shopt -s extglob
IGNOREEOF=yes
Run Code Online (Sandbox Code Playgroud)

~/.zshenv

# Put any zsh-specific settings for non-interactive and interactive sessions, e.g.
setopt braceexpand
setopt promptsubst
setopt shwordsplit
Run Code Online (Sandbox Code Playgroud)

~/.zshrc

test -f ~/.shrc && source ~/.shrc

# Put any zsh-specific interactive settings here, e.g.
HISTFILE=~/.zsh_history
setopt ignoreeof
Run Code Online (Sandbox Code Playgroud)

~/.profile

# Interactive sub-shells source .env, unless this is bash or zsh,
# because they already sourced .env in .bashrc or .zshrc.
if test -z "$BASH_VERSION" -a -z "$ZSH_VERSION" || test -n "$BASH_VERSION" -a \( "${BASH##*/}" = "sh" \)
then
    test -f "$HOME"/.env && . "$HOME"/.env
fi

# The name is confusing, but $ENV is ksh's config file for interactive sessions,
# so it's equivalent to .bashrc or .zshrc.
# Putting this here makes running an interactive ksh from any login shell work.
test -f "$HOME"/.shrc && export ENV="$HOME"/.shrc

# Put any login shell specific commands here, e.g.
ssh-add
stty -ixon
Run Code Online (Sandbox Code Playgroud)

~/.bash_profile

source ~/.bashrc
source ~/.profile
Run Code Online (Sandbox Code Playgroud)

~/.zlogin

# zsh sources .zshrc automatically, only need to source .profile
source ~/.profile
Run Code Online (Sandbox Code Playgroud)

~/.zprofile

(empty)
Run Code Online (Sandbox Code Playgroud)

如果您对系统具有 root 访问权限,另一种方法是设置pam_env.

你可以放

session optional pam_env.so user_envfile=.env
Run Code Online (Sandbox Code Playgroud)

在相关/etc/pam.d文件中(例如/etc/pam.d/common-session在 Debian 上),然后当用户登录时,PAM将从~/.env.

请注意,pam_env基本上只支持VAR=value条目。

更多信息:


ech*_*hox 15

没有像不同 shell 的环境配置文件这样的东西,因为它甚至 shell 特定于它们的定义方式。

在 csh 中,您setenv在 bash 中使用export来定义它们。

无论如何,您可以编写自己的配置文件并将其包含source在 shell 的点文件中。