zsh

Getting started with zsh

Remarks#

zsh is a POSIX-compatible shell, and a popular alternative to the Bourne shell (sh) and bash.

Its key feature is a focus on a high level of customization by the user, which has led to an active community of developers creating extensions for zsh, including custom, more informative prompt status lines, often integrating with system services.

Many configurations with large sets of sensible defaults and useful extensions exist online, including the popular oh-my-zsh and prezto.

Versions#

VersionRelease Date
5.3.12016-12-21
5.32016-12-12
5.22015-12-02
5.1.12015-09-11
5.12015-08-30
5.0.82015-05-31
5.0.02012-07-24
4.3.17 (beta)2012-02-23
4.2.72007-12-18
4.3.1 (beta)2006-02-28
4.2.02004-03-19
4.0.92003-12-19
4.1.1 (beta)2003-06-19
4.0.12001-06-01
3.1.92000-06-05
3.0.82000-05-16
3.1.6 (beta)1999-08-01
3.0.01996-08-15
2.6-beta211996-06-19
2.6-beta11994-10-16
2.5.01994-07-14
2.3.11993-02-20
2.21992-05-13
2.11991-10-24
2.01991-04-24
1.01990-12-15

Installation or Setup

Getting zsh

zsh is available on many UNIX-like platforms via their built-in package management systems. On the Debian and Ubuntu Linux distributions, zsh is available in the default package repositories and can be installed using:

$ sudo apt-get install zsh
# or, on newer Ubuntu distributions
$ sudo apt install zsh

On RPM-based distributions, zsh is also often available in the default package archives and can be installed using:

$ yum install zsh

On Fedora 22 and later:

$ dnf install zsh

On BSD systems, zsh can be installed using pkg:

$ pkg install zsh

On OpenBSD, zsh can be installed using pkg_add:

$ pkg_add zsh

On Arch Linux, zsh can be installed using pacman:

$ pacman -S zsh

On openSUSE, zsh can be installed using zypper:

$ zypper install zsh

On systems running macOS (OS X) zsh is already installed by default, although not set as default shell. You can also install newer versions via Homebrew:

$ brew install zsh

Alternatively, zsh’s source code can be obtained from the official website.

From there, the shell can be started by typing zsh at the prompt.

Making zsh your default shell

On most Linux and BSD systems, zsh may be set as the default shell for a user using the chsh command:

$ chsh -s shell [username]

Where

  • username is a real username (defaults to the current user if left out)
  • shell is the path to the zsh binary. The path should be listed in the /etc/shells file, which contains a list of allowed shells for use with chsh. Should zsh not be listed there - for example because you compiled and installed it from source - you will need to add a line with the absolute path to zsh first. You can get this path with which zsh (provided it is installed in a directory listed in PATH)

In order to see the changes log out once and log in. Open the terminal emulator and use

`echo $SHELL`

If it displays /bin/zsh then you have successfully changed the default shell to zsh.

Configuration

When starting Zsh, it’ll source the following files in this order by default:

  1. /etc/zsh/zshenv Used for setting system-wide environment variables; it should not contain commands that produce output or assume the shell is attached to a tty. This file will always be sourced, this cannot be overridden.

  2. $ZDOTDIR/.zshenv Used for setting user’s environment variables; it should not contain commands that produce output or assume the shell is attached to a tty. This file will always be sourced.

  3. /etc/zsh/zprofile Used for executing commands at start, will be sourced when starting as a login shell.

Note that on Arch Linux, by default it contains one line which source the /etc/profile.

/etc/profile This file should be sourced by all Bourne-compatible shells upon login: it sets up $PATH and other environment variables and application-specific (/etc/profile.d/*.sh) settings upon login.

  1. $ZDOTDIR/.zprofile Used for executing user’s commands at start, will be sourced when starting as a login shell.

  2. /etc/zsh/zshrc Used for setting interactive shell configuration and executing commands, will be sourced when starting as a interactive shell.

  3. $ZDOTDIR/.zshrc Used for setting user’s interactive shell configuration and executing commands, will be sourced when starting as a interactive shell.

  4. /etc/zsh/zlogin Used for executing commands at ending of initial progress, will be sourced when starting as a login shell.

  5. $ZDOTDIR/.zlogin Used for executing user’s commands at ending of initial progress, will be sourced when starting as a login shell.

  6. $ZDOTDIR/.zlogout Will be sourced when a login shell exits.

  7. /etc/zsh/zlogout Will be sourced when a login shell exits.

If $ZDOTDIR is not set, $HOME is used instead.

For general personal use, it is typical to edit the user’s .zshrc file for personal preferences

Aliases

To alias a command in you ~/.zshrc file, you can use the following syntax:

alias [alias-name]="[command-to-execute]"

For example, it is common to execute the command ls -a. You can alias this command as la as such:

alias la="ls -a"

After reloading the ~/.zshrc file, you will be able to type la and ls -a will be executed.

Directory Aliases

It is common to have certain folders that you cd into often. If this is the case, you can create aliasses to those directories to make cding to them easier. For example, the following will alias the Dropbox folder:

alias db="cd ~/Dropbox"

allowing you to enter db and change directories to ~/Dropbox.

Reload ZSH Configuration

zsh loads configuration from the ~/.zshrc file on startup. If you make changes to that file, you can either restart zsh or run the following command to reload the configuration.

. ~/.zshrc

You can alias this useful command in your ~/.zshrc like this:

alias reload=". ~/.zshrc"

This modified text is an extract of the original Stack Overflow Documentation created by the contributors and released under CC BY-SA 3.0 This website is not affiliated with Stack Overflow