How to switch from bash to zsh "for dummies"

Write tutorials for Linux Mint here
More tutorials on https://github.com/orgs/linuxmint/discu ... /tutorials and (archive) on https://community.linuxmint.com/tutorial
Forum rules
Don't add support questions to tutorials; start your own topic in the appropriate sub-forum instead. Before you post read forum rules
Post Reply
User avatar
Flemur
Level 20
Level 20
Posts: 10096
Joined: Mon Aug 20, 2012 9:41 pm
Location: Potemkin Village

How to switch from bash to zsh "for dummies"

Post by Flemur »

I've been playing with zsh for couple of days and found a lot of confusing information about switching to zsh (from bash, say) and using it. So here' what little I know.

1 - There's a thing called "oh-my-zsh" which supposedly sets up your ~/.zshrc file, but I found that gave a selection of super-ugly prompts, selected by name, like "alan1", and somehow prevented changing the prompt to something better.
Edit: I use a white background on terminals and the few prompts I tried were almost unreadable.

2 - There's another thing called zsh-newuser-install which sets up some settings - that I'm probably missing by using the .zshrc file below - but also provided ugly, unchangeable prompts.

So here's a bare bones version, using neither of the above:
Install zsh - probably already installed.
Make a ~/.zshrc file (the two products mentioned above do this, but I didn't like the result).

cat ~/.zshrc
Edit: updated version (03Mar'21)

Code: Select all

#
# ~/.zshrc 
#
HISTFILE=~/.config/zsh-histfile
HISTSIZE=4000
SAVEHIST=4000
bindkey -v
unset zle_bracketed_paste

export PS1="%F{red}Z:%f%d : "

export TIME_STYLE="long-iso"  # YYYY-MM-DD

# Tab completion: seems to work without these two lines?
# need them so that cd /u/sb/[tab]-> /usr/share
autoload -U compinit
compinit

# enable zsh calculator zcalc
autoload zcalc

# cd to dir by dir-name
setopt autocd

# associtate file suffix  with opener:
alias -s {txt,conf}=vi
alias -s {htm,html}=firefox
alias -s {jpg,jpeg,png,gif,bmp}=iview
#
alias -g  S=" | sort "
alias -g SN=" | sort -n"
alias -g SH=" | sort -h"
alias -g  G=" | grep "
alias -g  L=" | less "
#
alias zv="    vi ~/.zshrc"
alias zs="source ~/.zshrc"
#
alias  cdm='(){ cd /mnt/$1 }'
alias data='(){ cd /mnt/DATA/$1 }'
alias down='(){ cd /mnt/DATA/download/$1 }'
alias    h='(){ history "-"$1 }'  # return last N, passing postive N
#
alias hg='history 1 | grep ' # search all history
#
# copied from .bashrc - NONE of the below are zsh-specific
#
alias iv='iview '
alias sud='sudo env PATH=$PATH'
alias Su='su - '
#
export QUOTING_STYLE=literal
#
export LANG=en_US.UTF-8
export LANGUAGE=en_US.UTF-8
export LC_ALL="C"
export LC_COLLATE="C"
#
# EOF
There's also a ~/.zprofile file; dunno if you even need it, but I based mine on the ~/.bash_profile file:
cat .zprofile

Code: Select all

#
# ~/.zprofile

[[ -f ~/.zshrc ]] && . ~/.zshrc
Then in your bash session, to run zsh and source .zshrc, just type

Code: Select all

zsh
If you want to source ~/.zshrc after changing it, you have to type . ~/.zshrc or . ./.zshrc if you're at $HOME; it won't take . .zshrc (perhaps it would if "." was in the PATH...)

To make it permanent, enter these
chsh -l # list the shells
chsh -s /bin/zsh # use the FULL PATH of zsh returned from chsh -l

Now when you login or start a terminal, you'll be running zsh.

If you get hosed up, return to bash by typing

Code: Select all

bash
Good luck!
Last edited by Flemur on Wed Mar 03, 2021 8:37 am, edited 2 times in total.
Please edit your original post title to include [SOLVED] if/when it is solved!
Your data and OS are backed up....right?
User avatar
Termy
Level 12
Level 12
Posts: 4248
Joined: Mon Sep 04, 2017 8:49 pm
Location: UK
Contact:

Re: How to switch from bash to zsh "for dummies"

Post by Termy »

It's also worth keeping in mind that there are subtle differences in syntax between ZSH and BASH, which is important if you handle any sort of Shell code on the terminal or in Shell scripts.
I'm also Terminalforlife on GitHub.
User avatar
AndyMH
Level 21
Level 21
Posts: 13704
Joined: Fri Mar 04, 2016 5:23 pm
Location: Wiltshire

Re: How to switch from bash to zsh "for dummies"

Post by AndyMH »

and the benefits of zsh over bash?
Thinkcentre M720Q - LM21.3 cinnamon, 4 x T430 - LM21.3 cinnamon, Homebrew desktop i5-8400+GTX1080 Cinnamon 19.0
User avatar
Termy
Level 12
Level 12
Posts: 4248
Joined: Mon Sep 04, 2017 8:49 pm
Location: UK
Contact:

Re: How to switch from bash to zsh "for dummies"

Post by Termy »

AndyMH wrote: Wed Feb 24, 2021 4:01 pm and the benefits of zsh over bash?
There are some code-related benefits. It has nice defaults set, which a lot of people seem to like, despite the fact you can easily configure BASH to do the same. Saves hassle for user, I suppose. ZSH has really good completion support, even allowing an interactive menu select style for possible completions. ZSH seems to have better support for prompts, although I've yet to come across anything which really makes it worthwhile to switch to a different shell because of a prompt. ZSH has better alias support, too.

Beyond that, I couldn't tell you, because I've little experience in ZSH. Years ago, I used ZSH briefly, but stopped when I realised I wasn't getting anything new that I actually needed or in which I was really all that interested, plus it was impractical to familiarize myself with a shell in which I wasn't even programming, since most people program for BASH. Of course, YMMV, but that was just my experience. I still think it's a cool shell, and some of its coding bonuses are really handy, such as accessing an index in an array with just $Array[1], where in BASH, you'd need braces, like: ${Array[1]}

I think if you're looking at a new shell for mainly better and prettier interactivity, ZSH and FISH are great alternatives to the likes of BASH. FISH offers syntax highlighting and also really good completion support.
I'm also Terminalforlife on GitHub.
User avatar
AndyMH
Level 21
Level 21
Posts: 13704
Joined: Fri Mar 04, 2016 5:23 pm
Location: Wiltshire

Re: How to switch from bash to zsh "for dummies"

Post by AndyMH »

I'll stick with bash :)
Thinkcentre M720Q - LM21.3 cinnamon, 4 x T430 - LM21.3 cinnamon, Homebrew desktop i5-8400+GTX1080 Cinnamon 19.0
User avatar
Termy
Level 12
Level 12
Posts: 4248
Joined: Mon Sep 04, 2017 8:49 pm
Location: UK
Contact:

Re: How to switch from bash to zsh "for dummies"

Post by Termy »

Me too, Andy, but I recommend at least loading ZSH up, to see if it aligns with your tastes. :) It's easy to install and is super lightweight.
I'm also Terminalforlife on GitHub.
User avatar
shedyed
Level 4
Level 4
Posts: 309
Joined: Wed Feb 03, 2021 5:12 pm

Re: How to switch from bash to zsh "for dummies"

Post by shedyed »

The reason I'm switching to zsh is because of one distro that kinda insisted on it--and I got hooked. I still have a few bash scripts, of course.

Constant use of terminology also led me to this decision. Install it now from enlightenment.org and pick the nyanology theme :D
User avatar
Termy
Level 12
Level 12
Posts: 4248
Joined: Mon Sep 04, 2017 8:49 pm
Location: UK
Contact:

Re: How to switch from bash to zsh "for dummies"

Post by Termy »

I've used Terminology quite a bit over the years, but never felt compelled to use ZSH because of it; what made you so compelled?
I'm also Terminalforlife on GitHub.
Post Reply

Return to “Tutorials”