zsh with Cygwin

35

19

How can I configure zsh to be the default shell under Cygwin?

simao

Posted 2011-02-21T14:29:36.693

Reputation: 883

Answers

45

Unless you're using cygwin as a multiuser environment (in which case use chsh as you would under a standard environment.)

Otherwise, you just change cygwin.bat to run zsh -l -i instead of bash --login -i and it will run as a login shell.  

Of course, if you want to run multiple shells from startup, just create a set of .bat files to load different shells. (sh, ksh, csh, fish etc)

Update...

I felt I should update this to provide info on doing this without chsh but still doing it on the Unix end. Edit the /etc/passwd file and replace occurrences of /bin/bash with /bin/zsh. (This is effectively what chsh would do, but this way you'd do it for all users in one go.)

ocodo

Posted 2011-02-21T14:29:36.693

Reputation: 1 672

15There is no longer an /etc/passwd in cygwin by default, and chsh is no longer supported. This answer does not help (April 10, 2014). – Jeff Snider – 2015-04-10T14:51:06.567

Go home @THESorcerer you're drunk – ocodo – 2015-10-27T11:38:34.770

2@JeffSnider run mkpasswd -l -p "$(cygpath -H)" > /etc/passwd and edit the newly create /etc/passwd file – BrunoLM – 2016-03-29T21:38:28.367

17

If I read your question right, you're looking for something else than what chere supplies (which, granted, is pretty cool in its own right).

The current version of Cygwin doesn't have an /etc/passwd file, and the system I'm working on has Windows account information in a domain database out of my control. Consequently, chsh is no longer supported.

I also found that bash is not hard-coded into the startxwin script, nor is it hard-coded in any .bat file. Turns out you don't need to fiddle with .bat files at all.

Searching for how to change my shell, I found some advice about mkpasswd

I added it to the mix.

The man-page said:

SYNOPSIS
   mkpasswd [OPTIONS]...

OPTIONS
   Don't use this command to generate a local /etc/passwd file, unless you
   really need one.  See the Cygwin User's Guide for more information.

   -c,--current
          Print current user.

  DESCRIPTION
        The   mkpasswd  program can be used to create a        /etc/passwd
   file.  Cygwin doesn't need  this  file,        because  it  reads  user
   information  from  the Windows account databases,       but you can add
   an  /etc/passwd file, for instance       if your machine is often  dis‐
   connected from its domain controller.

        Note  that this information is static, in contrast to the informa‐
   tion       automatically gathered by Cygwin from  the  Windows  account
   databases.  If        you  change  the user information on your system,
   you'll need to regenerate       the passwd file for it to have the  new
   information.


        For  very  simple needs, an entry for the current user can be cre‐
   ated       by using the option  -c.

(I don't know why the spacing is so "off"...)

I then used the following command:

mkpasswd -c | sed -e 'sX/bashX/zshX' | tee -a /etc/passwd

The next time you open a Cygwin Terminal, it will go straight to zsh

And that, I think, is what you asked for.

David L. Paktor

Posted 2011-02-21T14:29:36.693

Reputation: 171

17

I just add the bin directory in the cygwin directory to my Windows Path Environment Variable and then either create a shortcut to:

mintty.exe -i /Cygwin-Terminal.ico /bin/zsh --login

or just change the default cygwin start menu shortcut to the same.

Justin Buser

Posted 2011-02-21T14:29:36.693

Reputation: 1 147

2I had to use /bin/zsh instead of zsh. – kraiz – 2015-09-06T16:44:34.503

1the bin directory in the cygwin directory to my Windows Path Environment Variable works without this change for me – ruslo – 2015-11-29T11:05:07.127

the i is not needed because it will be a login shell and not interactive. – Timo – 2018-05-26T08:45:46.110

7

Copied from my answer on Stack Overflow:


Instead of creating a passwd file, which Cygwin recommends against1, you could edit /etc/nsswitch.conf. Add or edit the following line:

db_shell: /usr/bin/zsh

The down/up side of this method is that, if you have multiple users, this change affects all of them. The up/up side is that it's dead simple. The only catch is that you have to restart Cygwin.

If you do use mkpasswd after this change, it will use your new default shell for all users that are allowed to log on.


1 The mkpasswd documentation says this:

Don't use this command to generate a local /etc/passwd file, unless you really need one. See the Cygwin User's Guide for more information.

I can't really find any solid reasoning in the user's guide, other than a mention that you'll have to regenerate the /etc/passwd and /etc/group files if your users and groups change, which I suppose is a decent enough reason. I can say that the process is somewhat error prone for newbies.

P Daddy

Posted 2011-02-21T14:29:36.693

Reputation: 391

This should be marked as accepted. – Arda – 2016-12-18T18:00:58.903

4

Assuming you're interested in changing the shell used in mintty/Cygwin Terminal, it first checks the SHELL environment variable, then the (now nonexistent) passwd file, then falls back to /bin/sh, which is what it seems to be doing by default.

I was able to set the SHELL environment variable in Windows to /usr/bin/zsh and it worked without any other changes. I just changed it for my Windows user, not globally.

Setting an environment variable probably varies for different versions of Windows. Try searching for "environment" in your Windows Control Panel. For me, under Windows 7, it was Control Panel → System → Advanced System Settings, which brings up the System Properties control panel/dialog, then the Advanced tab, then the Environment Variables button, which brings up the Environment Variables dialog, then create a new user variable named SHELL with the value /usr/bin/zsh. Then OK back out of all of that and start a new mintty.

wfaulk

Posted 2011-02-21T14:29:36.693

Reputation: 5 692

Now this should be upvoted profusely! The simplest way that does not mess up with Cygwin's internal machinations, and easily reversible! – pepoluan – 2016-09-24T13:20:34.030

1

Just thought I'd add this answer in case it helps anyone who's looking for it. If you use chere (which adds a "Bash prompt here" option to the right-click context menu of any folder), you can set it to zsh ("Zsh Prompt Here") with:

chere -i -t mintty -s zsh

Mike H-R

Posted 2011-02-21T14:29:36.693

Reputation: 662

0

If you want to use Cygwin shell inside the IDE Intellij, then use following setup inside settings/tools/terminal

c:\cygwin\bin\zsh.exe -l -i

This will embed the cygwin shell inside intellij instead of spawning a new window

serup

Posted 2011-02-21T14:29:36.693

Reputation: 111