Home > Net >  profile and zprofile not both executed for login shellss?
profile and zprofile not both executed for login shellss?

Time:09-18

When I open a shell, .zprofile is automatically sourced but .profile not. I verify this because

.zprofile contains

echo "we are in zprofile"
...

.profile contains

echo "we are in profile"
...

and when opening a new shell I get only

we are in zprofile

which seems to be in contradiction with this post which says that .zprofile is sourced for login shells only.

so should both be not be sourced when opening a "normal" shell?

Also when I do su - johnsmith or su johnsmith --login, the .profile is not beeing sourced and I don't understand why.

CodePudding user response:

Zsh ignores .profile. That file is not mentioned in the Zsh docs or the post you linked.

If you want to use .profile in .zprofile, you must manually source .profile.

Ideally you would also run it in emulate sh mode because Zsh is not a strict superset of Posix Sh. You can use an anonymous function for this:

if [[ -f ~/.profile ]]; then
  function {
    emulate -L sh
    source ~/.profile
  }
fi

If your .zprofile is running on every new shell (not just on first login), then you must be starting new login shells. This is atypical and arguably incorrect on Linux, but it is the default and expected behavior on MacOS.

  • Related