150

When zsh is set as a login shell on Mac OS X, when it is started by iTerm, zsh doesn't consider that it's being run as a login shell, although it's started as ‘-zsh’ (‘-’ is put as the first character of arg[0]) which is supposed to mean that it should start as a login shell.

So, when I set the login shell to bash, bash recognizes this first ‘-’ in $0 and runs as a login shell, but zsh doesn't, although it seems that it should.

Is there a way to either make zsh recognize the ‘-’ in the arg[0], or make iTerm run the shell with a --login command line argument?

1
  • 2
    I know it's been a while since this was asked, but please consider accepting @Harold Putman's answer. It's the only on which addresses the question asked. Dec 24, 2019 at 23:54

6 Answers 6

219
chsh -s $(which zsh)

You'll be prompted for your password, but once you update your settings any new iTerm/Terminal sessions you start on that machine will default to zsh.

11
  • 2
    Please, read the question first. The problem is not in setting the default shell, the problem is that zsh isn't detecting that it is started as a login shell. Dec 1, 2009 at 3:59
  • 1
    Ah, my apologies. Have you tried using the -i flag to force zsh to start in interactive mode? Dec 1, 2009 at 20:00
  • 11
    His answer does make zsh run as a login shell on Mac OS X in iTerm. It just doesn't go about doing it the way you describe trying to. But it solves the problem that you question (in the title, rather than in the body) presents. The question you end with in the body is a completely different question. If that's your real question (rather than how to make zsh run as login shell) then perhaps you should reword the question in the title.
    – iconoclast
    Feb 10, 2011 at 17:45
  • 46
    If chsh tells you something like "nonstandard shell", it means you have to add the path of the new shell to /etc/shells. This can happen if you're changing, for example, to a version of zsh installed by homebrew, which puts it in /usr/local/bin/zsh.
    – shovavnik
    Oct 30, 2014 at 20:32
  • 1
    chsh -s $(which zsh) can be used to ensure correct path. Nov 26, 2014 at 11:00
112

In iTerm -> Preferences -> Profiles Tab -> General section set Command to: /bin/zsh --login

/bin/zsh --login

6
  • 1
    iterm2 doesn't seem to have this option in the General tab. Any idea how to achieve in iterm2 without changing login shell? Apr 14, 2014 at 17:08
  • 6
    It's general tab on the Profiles Tab. Apr 15, 2014 at 18:09
  • Aha! That's confusing that there is both a General Tab and a Profiles > General. Apr 20, 2014 at 18:09
  • 2
    or in my case /usr/local/bin/zsh --login
    – Yar
    Jul 1, 2016 at 17:01
  • 1
    Why do we pass the --login flag with the command option rather than selecting the Login shell option?
    – Andrew
    Mar 14, 2022 at 23:39
60

Go to the Users & Groups pane of the System Preferences -> Select the User -> Click the lock to make changes (bottom left corner) -> right click the current user select Advanced options... -> Select the Login Shell: /bin/zsh and OK

2
  • 3
    This saved my day. I am on osx mavericks.
    – qed
    Jul 17, 2014 at 23:27
  • This works for the default zsh not for the one installed by brew.
    – Imam Bux
    Dec 15, 2018 at 10:21
21

The command to change the shell at startup is chsh -s <path_to_shell>. The default shells in mac OS X are installed inside the bin directory so if you want to change to the default zsh then you would use the following

chsh -s /bin/zsh

If you're using different version of zsh then you might have to add that version to /etc/shells to avoid the nonstandard shell message. For example if you want home-brew's version of zsh then you have to add /usr/local/bin/zsh to the aforementioned file which you can do in one command sudo sh -c "echo '/usr/local/bin/zsh' >> /etc/shells" and then run

chsh -s /usr/local/bin/zsh

Or if you want to do the whole thing in one command just copy and paste this if you have zsh already installed

sudo sh -c "echo '/usr/local/bin/zsh' >> /etc/shells" && chsh -s /usr/local/bin/zsh
3
  • 3
    I don't think it was fair to downvote this answer. Props as it is the only one that mentions /etc/shells in the body of the answer itself.
    – heymatthew
    Aug 30, 2016 at 5:51
  • 1
    @heymatthew the reason it is being downvoted is because it didn't answer the question asked. It answered a completely different unrelated question.
    – Tabitha
    May 16, 2019 at 15:54
  • chsh: /usr/local/bin/zsh is an invalid shell. I had to do: sudo sh -c "echo '/usr/bin/zsh' >> /etc/shells" && chsh -s /usr/bin/zsh
    – alper
    Aug 11, 2021 at 22:27
17

Have you tried editing the shell entry in account settings.

Go to the Accounts preferences, unlock, and right-click on your user account for the Advanced Settings dialog. Your shell should be /bin/zsh, and you can edit that invocation appropriately (i.e. add the --login argument).

3
  • Unfortunately, that doesn't work: I can set whichever shell I like using chsh (as long as I have this shell listed in /etc/shells), however one cannot add command line arguments to the shell the Advanced preferences dialog (iTerm fails to start the shell up). Aug 14, 2009 at 9:44
  • Some of the syntax has changed in OS X but the gist is to set your default shell to be /bin/zsh as Brian indicates above Jun 17, 2012 at 20:17
  • 7
    I love SO. I've used this answer just now, and only then realised I wrote it nearly 4 years ago! Jun 24, 2013 at 10:53
-3

Use the login utility to create a login shell. Assume that the user you want to log in has the username Alice and that zsh is installed in /opt/local/bin/zsh (e.g., a more recent version installed via MacPorts). In iTerm 2, go to Preferences, Profiles, select the profile that you want to set up, and enter in Command:

login -pfq Alice /opt/local/bin/zsh

See man login for more details on the options.

1
  • This is 100% not an answer. Yes will change which shell you spawn by default, but that isn't an answer as to how to create a "login" instance of a shell, i.e. resource all variables from scratch.
    – Tabitha
    May 16, 2019 at 15:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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