Zsh not hitting ~/.profile

.profile vs. .zprofile

Zsh runs ~/.zprofile, not ~/.profile, when it is invoked as a login shell. The reason is that zsh has enough incompatibilities with standard shells to break scripts.

Zsh does run ~/.profile if it is called as sh or ksh. But if your objective is to get a zsh prompt when you log in, that's no help.

You can make /bin/sh your login shell and include export SHELL=/bin/zsh in your ~/.profile. Then when you open a terminal, the terminal will launch zsh (except for a few terminal emulators don't obey the $SHELL setting). But you will still have sh when you log in over ssh. This can be remedied by including exec zsh at the end of ~/.profile (this replaces the running shell by zsh), but you need to be careful only to do that for interactive logins and not when ~/.profile is included from other scripts such as the X session start (a good test is the name of the parent process obtained by ps -o comm= $PPID: if it's sshd or su, then it's safe to exec).

The easiest solution to both work with zsh and run ~/.profile is to create a ~/.zprofile that enters sh emulation mode while it runs ~/.profile:

emulate sh
. ~/.profile
emulate zsh

If you have a recent enough zsh (on Ubuntu, that means since lucid, I think), you can simplify this to emulate sh -c '. ~/.profile'.

.zprofile vs. .zshrc

The file ~/.profile is loaded by login shells. The login shell is the first process that is started when you log in in text mode, for example on a text console or via ssh. By default, on most Linux machines, the login shell is bash, but you can change it with the chsh command, or through some other tool such as the “User Settings” in Ubuntu. When it's a login shell, bash reads ~/.bash_profile if it exists and ~/.profile, whereas zsh reads only ~/.zprofile (because its syntax is not completely compatible with a traditional sh). Under most configurations, ~/.profile is also loaded by the X session startup scripts when you log in in a graphical display manager.

When you start a terminal emulator and get a shell prompt, or when you start a shell explicitly, you get a shell that is not a login shell. Since ~/.profile (or ~/.zprofile) is for commands that you want to execute when you log in, a non-login shell doesn't read this file. Instead, when you start an interactive zsh, it reads ~/.zshrc. (Zsh reads ~/.zshrc in all interactive shells, whether they are login shells are not; bash, strangely, never reads ~/.bashrc in login shells.)

Typically, ~/.profile contains environment variable definitions, and might start some programs that you want to run once when you log in or for the whole session; ~/.zshrc contains things that must be done for every shell instance, such as alias and function definitions, shell option settings, completion settings, prompt settings, key bindings, etc.


Short answer for impatient people:

  1. ~/.profile is not loaded by zsh at login.
  2. zsh loads ~/.zprofile at login.
  3. zsh loads ~/.zshrc when starting a new terminal session.

Need more info? Look at Gilles' superb answer!


In addition to Gilles's answer, with a reasonably recent version of zsh you can do this:

[[ -e ~/.profile ]] && emulate sh -c 'source ~/.profile'

...Which will source the .profile file with zsh's sh-mode in effect. And it's only active during the source. So you do not have to save the current option state in order to replay it again after sourcing.

Tags:

Ubuntu

Zsh