DEV Community

Cover image for From bash to zsh. My little adventure
Max Belsky
Max Belsky

Posted on • Edited on

From bash to zsh. My little adventure

A few months ago Apple announced the upcoming version of its desktop OS macOS Catalina, which is going to replace the default command line shell bash with zsh for all newly created accounts. This was the start of my “adventure”. It wasn’t so long, however it was full of pitfalls. So I came here in the hope to save a few hours for someone.

First of all lets change a command line shell for your user. To do that we need locate zsh and set as default shell:

# get path to `zsh` on your machine and use it with `chsh` command
$ which zsh
/bin/zsh
$ chsh -s /bin/zsh $USER
Enter fullscreen mode Exit fullscreen mode

Restart Terminal.app and make sure that your changes applied:

$ ps -p $$ -oargs=
-zsh
Enter fullscreen mode Exit fullscreen mode

Now we can make some customizations. Every day I operate with git from terminal so there are two important things for me: git-prompt and git-autocomplete. Zsh provides git-autocomplete out of the box. So there is some commands to set up prompt:

$ cd ~/
$ curl -o .git-prompt.sh https://raw.githubusercontent.com/git/git/master/contrib/completion/git-prompt.sh
$ touch ~/.zshrc
Enter fullscreen mode Exit fullscreen mode

We almost done 😁 Open ~/.zshrc in your favourite text editor and put there:

#!/bin/zsh

# Set up git-prompt
source ~/.git-prompt.sh
setopt PROMPT_SUBST; PS1='%~ $(__git_ps1 "(%s)")\$ '

autoload -U compinit; compinit

# If you have aliases or $PATH extensions  in your .bash_profile paste it below
Enter fullscreen mode Exit fullscreen mode

Restart your terminal one more time and enjoy your new shell.

A terminal's preview after restart

Top comments (2)

Collapse
 
bak3y profile image
Bak3y

Did you have any existing shell scripts you had to rewrite due to the swap?

Collapse
 
mbelsky profile image
Max Belsky

I had no bash scripts, so can't share my experience about that. However as I know you may put shebag as first line in a bash script and it will run as bash.

#!/usr/bin/env bash