Is ~/.bashrc the only place to specify user specific environment variables, aliases, modifications to PATH variable, etc?

I ask because it seems that ~/.bashrc seems to be bash-only, but other shells exist too…

up vote 86 down vote accepted

The file $HOME/.profile is used by a number of shells, including bash, sh, dash, and possibly others.

From the bash man page:

When bash is invoked as an interactive login shell, ... it first reads and executes commands from the file /etc/profile, if that file exists. After reading that file, it looks for ~/.bash_profile, ~/.bash_login, and ~/.profile, in that order, and reads and executes commands from the first one that exists and is readable.

csh and tcsh explicitly don't look at ~/.profile but those shells are kinda antiquated.

  • 7
    Zsh by default does not read .profile. That's why I deleted my previous answer stating this. Zsh only reads .profile when it is invoked by a symbolic link named sh. – fschmitt Oct 13 '10 at 15:48
  • 10
    tcsh is still popular in some enviroments. – Maciej Piechotka Oct 13 '10 at 23:01
  • 1
    fschmitt: thanks for the correction; fixed. Maciej Piechotka: I have no doubt that's true; however it is also possible (though complicated) to make *rc scripts that import particular other rc scripts based on the shell they are running under. – msw Oct 14 '10 at 3:27
  • 1
    For this way to work, users need to ensure that each shell is a login shell. For example, in Gnome Terminal, go to Profile -> Title and Command, and enable Run command as a login shell. You also need to remove ~/.bash_profile, or make it source ~/.profile. – Mikel Nov 26 '13 at 20:07
  • @fschmitt You can also source $HOME/.profile from inside your Zsh .zshrc, too. I tend to put all my portable shell things in .profile and then I can share it around any environment I might hop between. – Benjamin R May 31 '17 at 3:51

~/.profile is the right place for environment variable definitions and for non-graphical programs that you want to run when you log in (e.g. ssh-agent, screen -m). It is executed by your login shell if that is a Bourne-style shell (sh, ksh, bash). Zsh runs ~/.zprofile instead, and Csh and tcsh run ~/.login.

If you log in under an X display manager (xdm, gdm, kdm, ...), whether ~/.profile is run depends how your display manager and perhaps desktop environment were configured by your distribution. If you log in under a “custom session”, that usually executes ~/.xsession.

~/.bashrc is the right place for bash-specific settings, such as aliases, functions, shell options and prompts. As the name indicates, it is specific to bash; csh has ~/.cshrc, ksh has ~/.kshrc, and zsh has <drumroll> ~/.zshrc.

See also:
Difference between .bashrc and .bash_profile
Which setup files should be used for setting up environment variables with bash?
Zsh not hitting ~/.profile

  • Don't forget zsh also has .zlogin in addition to .zprofile which runs after .zshrc (but only for login shells). See the ZSH FAQ – Geeb Jan 23 '14 at 14:23

There is no common file, but you can make every shell read from a common file.

  1. bash reads from .bash_profile or .bashrc
  2. zsh reads from .zprofile and .zshrc
  3. ksh reads from .profile or $ENV

So here's what I do:

~/.env

# Put environment variables here, e.g.
PATH=$PATH:$HOME/bin

~/.shrc

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

# Put interactive shell setup here, e.g.
alias ll='ls -l'
PS1='$PWD$ '
set -o emacs

~/.bashrc

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

# Put any bash-specific settings here, e.g.
HISTFILE=~/.bash_history
shopt -s extglob
IGNOREEOF=yes

~/.zshenv

# Put any zsh-specific settings for non-interactive and interactive sessions, e.g.
setopt braceexpand
setopt promptsubst
setopt shwordsplit

~/.zshrc

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

# Put any zsh-specific interactive settings here, e.g.
HISTFILE=~/.zsh_history
setopt ignoreeof

~/.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

~/.bash_profile

source ~/.bashrc
source ~/.profile

~/.zlogin

# zsh sources .zshrc automatically, only need to source .profile
source ~/.profile

~/.zprofile

(empty)

If you have root access to the system, another way is to set up pam_env.

You can put

session optional pam_env.so user_envfile=.env

in the relevant /etc/pam.d file (e.g. /etc/pam.d/common-session on Debian), and then when the user logs in, PAM will read environment variables from ~/.env.

Note that pam_env basically only supports VAR=value entries.

More info:

There is no such thing like a environment config file for different shells, because its even shell specific how they are defined.

In csh you use setenv in bash you use export to define them.

Anyway you could write your own config file and include it with source in the dotfiles of your shells.

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.