Zsh not hitting ~/.profile

167

92

I've just installed zsh on my Ubuntu system. It seems zsh isn't executing ~/.profile on init. To my best understanding, this should be an automatic behavior. What am I missing?

shmichael

Posted 2010-09-12T07:20:00.763

Reputation: 2 033

Answers

234

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

Gilles 'SO- stop being evil'

Posted 2010-09-12T07:20:00.763

Reputation: 58 319

Great answer! I've created ~/.zprofile and put the line as you suggested, but it doesn't seem to do anything. Perhaps I should put it in .zshrc? – shmichael – 2010-09-13T12:21:03.730

1@shmichael: Given your reaction, I may not have interpreted your question the way you intended it. Does the second section I've added to my answer help? – Gilles 'SO- stop being evil' – 2010-09-13T18:11:53.883

yes, hit the spot. So if I understand correctly I would want e.g. PATH declarations on .zprofile and aliases on .zshrc. – shmichael – 2010-09-15T15:19:01.563

@user1419674 Thank you for the correction, but do not add a change log to posts. The site has a built-in revision history. – Gilles 'SO- stop being evil' – 2014-03-17T08:28:31.637

83

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!

Karl Morrison

Posted 2010-09-12T07:20:00.763

Reputation: 1 507

1love this TLDR. – hassan789 – 2019-12-28T02:35:17.950

22

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.

Frank Terbeck

Posted 2010-09-12T07:20:00.763

Reputation: 321

I have this in my ~/.zprofile, but for some reason the statements in ~/.profile are not available in my shell. Ubuntu 15.04; zsh 5.0.7 – friederbluemle – 2015-09-15T11:08:42.897

Hard to diagnose without code. – Frank Terbeck – 2016-03-27T16:10:19.697

I just tried again, and it looks like it is working now. Not sure if it was fixed at some point or what happened. zsh version is 5.1.1 now. – friederbluemle – 2016-03-28T03:41:41.333

5

I have .zprofile compatible with .profile (contains only PATH changes), hence only needing:

ln -s .profile .zprofile

Igor Shubovych

Posted 2010-09-12T07:20:00.763

Reputation: 151

1

The zsh documentation I have on hand only mentions /etc/profile and ~/.profile for login shells in sh/ksh compatibility mode.

% zsh --version
zsh 4.3.10 …
% cat ~/.profile
echo 'Running ~/.profile...'

Native mode login shell (argv[0] starts with -) does not use ~/.profile (it will use ~/.zprofile though):

% zsh -c 'exec -a -zsh zsh' </dev/null

(no output)

sh/ksh compatability mode login shells uses .profile:

% zsh -c 'exec -a -sh zsh' </dev/null
Running ~/.profile...
% zsh -c 'exec -a -ksh zsh' </dev/null
Running ~/.profile...

Chris Johnsen

Posted 2010-09-12T07:20:00.763

Reputation: 31 786