Setup Zsh As Your Default Shell

jakewies

Jake Wiesler

Posted on November 21, 2021

Setup Zsh As Your Default Shell

If you're switching from Bash to Zsh and you want to know how to launch new terminal sessions in Zsh, you've found the right blog post. I've had to do this a few times over the years, and it was always a struggle. Recently I started working on a portable development environment and I learned how to set Zsh as my default shell the right way. Here's how:

Prerequisite: Install Zsh

How you install Zsh is up to you. Checkout this GitHub gist for some OS-specific options. The following steps will work for any Linux/Unix-based machine. I am not aware of how to accomplish this on Windows.

Step 1

The first step is to tell your terminal that Zsh can be used as a valid login shell. You do this by updating a special file named /etc/shells. If you cat this file:

cat /etc/shells
Enter fullscreen mode Exit fullscreen mode

You will see a list of filepaths that each point to an executable file.

# List of acceptable shells for chpass(1).
# Ftpd will not allow users to connect who are not using
# one of these shells.

/bin/bash
/bin/csh
/bin/dash
/bin/ksh
/bin/sh
/bin/tcsh
Enter fullscreen mode Exit fullscreen mode

The output above may vary depending on your circumstance. One of these executables is your current shell. You can see your current shell by running:

echo $SHELL
Enter fullscreen mode Exit fullscreen mode

The output of the command above will be an executable file that resides within the /etc/shells list. For example, if I run the echo command above, the output I see is:

/bin/bash
Enter fullscreen mode Exit fullscreen mode

Locate Zsh's executable filepath

We need to append the Zsh exectuable to this list, but before doing that we need to know where it is. To find its location you can run the following command:

command -v zsh
Enter fullscreen mode Exit fullscreen mode

command -v will print the absolute filepath used when invoking the argument provided, in this case zsh. This gives us the path to Zsh's exectuable file. The location may vary depending on your installation method.

For example, running the command above may yield:

/bin/zsh
Enter fullscreen mode Exit fullscreen mode

If you see no output at all, then you probably don't have Zsh installed. See Prerequisites.

Append Zsh's executable filepath to /etc/shells

Once we have the location of Zsh's exectuable file, we can "pipe" it to another command:

sudo tee -a /etc/shells
Enter fullscreen mode Exit fullscreen mode

The tee utility, which we are running in sudo, takes an input and appends it to /etc/shells.

Together, the full command is:

command -v zsh | sudo tee -a /etc/shells
Enter fullscreen mode Exit fullscreen mode

The | is the pipe symbol. It takes the output of the left-hand side and uses it as the input for the right-hand side. In psuedo, what's happening is:

  1. Locate the filepath of Zsh
  2. Append the filepath to /etc/shells

After running the full command you should see Zsh's executable at the bottom of /etc/shells.

Step 2

Now that we've told our terminal that Zsh can be used as a valid login shell, we need to explicitly use it as the default. This can be accomplished with the following command:

sudo chsh -s $(which zsh) $USER
Enter fullscreen mode Exit fullscreen mode

The command above is chsh -s, or "change shell". We are running it in sudo mode for elevated permissions, and we pass it two arguments:

  1. $(which zsh) - The shell we want to use as the default
  2. $USER - The user whose default shell we are changing, ie. - you.

After running this command, the next time you start a new terminal session you will see Zsh! To confirm, you can run:

echo $SHELL
Enter fullscreen mode Exit fullscreen mode

Conclusion

And that's it! Running the commands defined in steps 1 and 2 will turn Zsh into your new default shell. If you have any further questions, or maybe you have a better way of doing this, let me know on Twitter!

💖 💪 🙅 🚩
jakewies
Jake Wiesler

Posted on November 21, 2021

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

Setup Zsh As Your Default Shell
webdev Setup Zsh As Your Default Shell

November 21, 2021