How to switch from bash to zsh "for dummies"

Write tutorials here
There are more tutorials here http://community.linuxmint.com/tutorial/welcome
Forum rules
Please don't add support questions to tutorials,start your own thread in the appropriate sub-forum instead. Before you post please read this
Post Reply
User avatar
Flemur
Level 19
Level 19
Posts: 9198
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 6
Level 6
Posts: 1465
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 use Linux Mint 18.3 with Cinnamon in a VirtualBox VM for testing & sandboxing.

I'm LearnLinux (LL) on YouTube: https://www.youtube.com/c/learnlinux
I'm also terminalforlife (TFL) on GitHub: https://github.com/terminalforlife
User avatar
AndyMH
Level 15
Level 15
Posts: 5939
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?
Homebrew i5-8400+GTX1080 Cinnamon 19.0, 3 x Thinkpad T430 Cinnamon 19.0, i7-3632 , i5-3320, i5-3210, Thinkpad T60 19.0 Mate
User avatar
Termy
Level 6
Level 6
Posts: 1465
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 use Linux Mint 18.3 with Cinnamon in a VirtualBox VM for testing & sandboxing.

I'm LearnLinux (LL) on YouTube: https://www.youtube.com/c/learnlinux
I'm also terminalforlife (TFL) on GitHub: https://github.com/terminalforlife
User avatar
AndyMH
Level 15
Level 15
Posts: 5939
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 :)
Homebrew i5-8400+GTX1080 Cinnamon 19.0, 3 x Thinkpad T430 Cinnamon 19.0, i7-3632 , i5-3320, i5-3210, Thinkpad T60 19.0 Mate
User avatar
Termy
Level 6
Level 6
Posts: 1465
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 use Linux Mint 18.3 with Cinnamon in a VirtualBox VM for testing & sandboxing.

I'm LearnLinux (LL) on YouTube: https://www.youtube.com/c/learnlinux
I'm also terminalforlife (TFL) on GitHub: https://github.com/terminalforlife
Post Reply

Return to “Tutorials”