iTranslated by AI

The content below is an AI-generated translation. This is an experimental feature, and may contain errors. View original article
🐚

Learning from the History of zsh: Origin of the Name and a Complete Guide to .zshrc Commands

に公開

Introduction

The default shell on macOS switched from Bash to zsh starting with macOS Catalina in 2019. While many developers use zsh every day, many might struggle if asked, "What does 'zsh' actually mean?" or "What does the 'rc' in '.zshrc' stand for?"

In this article, we will explore the historical background and origin of the name 'zsh,' and systematically explain configuration files like '.zshrc' along with frequently used commands.


The History of zsh

Origin: 1990, Princeton University

zsh was developed in 1990 by Paul Falstad while he was a student at Princeton University. Version 1.0 was released by being posted to the alt.sources Usenet newsgroup.

The mainstream shells of that time were as follows:

Shell Introduction Year Developer
sh (Bourne Shell) 1979 Stephen Bourne
csh (C Shell) 1979 Bill Joy
tcsh 1983 Ken Greer
ksh (Korn Shell) 1983 David Korn
bash (Bourne Again Shell) 1989 Brian Fox
zsh (Z Shell) 1990 Paul Falstad

zsh was designed as a "best of all worlds" shell that incorporated the excellent features of bash, ksh, and tcsh while adding its own unique extensions.

The Origin of the Name: What does "Z" stand for?

The "Z" in zsh comes from the login ID zsh of Zhong Shao, who was a professor at Princeton University at the time. Paul Falstad thought Shao's login name would make a good shell name; Shao himself was not involved in the development.

In other words, zsh = Z Shell. While some interpret the "Z" as the last letter of the alphabet, implying the "ultimate shell," the name officially originates from a person's login ID.


Configuration File Names and Roles

What does the "rc" in ".zshrc" stand for?

"rc" is an abbreviation for "Run Commands." This traces back to the 1965 MIT CTSS (Compatible Time-Sharing System). CTSS had a mechanism called RUNCOM that allowed users to define a list of commands to be executed automatically upon startup. This convention was passed down to Unix, and it remains in configuration file names like .bashrc, .vimrc, and .zshrc.

List of zsh Configuration Files

zsh reads different files depending on the shell's startup mode.

File Timing of Loading Main Purpose
.zshenv Always (on every zsh startup) Environment variables (e.g., PATH)
.zprofile At login shell startup Login initialization (equivalent to .bash_profile)
.zshrc At interactive shell startup Aliases, prompt settings, plugins
.zlogin At login shell startup (after .zprofile) Processes to run after login
.zlogout At login shell exit Cleanup processing

Loading Order

For login shells:
.zshenv → .zprofile → .zshrc → .zlogin

For non-login interactive shells:
.zshenv → .zshrc

For script execution:
.zshenv only

Frequently Used Settings in .zshrc

1. Alias

# Basic aliases
alias ll='ls -la'
alias la='ls -a'
alias ..='cd ..'
alias ...='cd ../..'

# Git-related
alias gs='git status'
alias ga='git add'
alias gc='git commit'
alias gp='git push'
alias gl='git log --oneline --graph'

# Safety measures
alias rm='rm -i'
alias cp='cp -i'
alias mv='mv -i'

2. Environment Variables and PATH

# Adding to PATH (zsh-specific array syntax)
typeset -U path  # Automatically remove duplicates
path=(
  $HOME/.local/bin
  /usr/local/bin
  $path
)

# Editor settings
export EDITOR='vim'
export VISUAL='code'

# Language settings
export LANG='ja_JP.UTF-8'

3. History Settings

HISTFILE=~/.zsh_history
HISTSIZE=10000
SAVEHIST=10000

# Useful options
setopt HIST_IGNORE_DUPS      # Do not record sequential duplicate commands
setopt HIST_IGNORE_ALL_DUPS  # Remove all historical duplicates
setopt HIST_REDUCE_BLANKS    # Remove excessive whitespace
setopt SHARE_HISTORY         # Share history between multiple terminals
setopt HIST_IGNORE_SPACE     # Do not record commands that start with a space

4. Completion

zsh's completion system is extremely powerful and is a major differentiator from bash.

# Initialize the completion system
autoload -Uz compinit && compinit

# Completion options
setopt AUTO_MENU              # Menu selection with repeated Tab presses
setopt AUTO_COMPLETE          # Automatic completion
zstyle ':completion:*' menu select  # Enable selection with arrow keys

# Case-insensitive completion
zstyle ':completion:*' matcher-list 'm:{a-z}={A-Z}'

# Add colors to completion candidates
zstyle ':completion:*' list-colors ${(s.:.)LS_COLORS}

5. Customizing the Prompt

# Simple prompt
PROMPT='%F{green}%n@%m%f:%F{blue}%~%f$ '

# Prompt with Git branch display
autoload -Uz vcs_info
precmd() { vcs_info }
zstyle ':vcs_info:git:*' formats '%F{yellow}(%b)%f'
setopt PROMPT_SUBST
PROMPT='%F{green}%~%f ${vcs_info_msg_0_} $ '

Main variables available for the prompt:

Variable Meaning
%n Username
%m Hostname
%~ Current directory (abbreviated with ~)
%d Current directory (full path)
%T Time (HH:MM)
%F{color}...%f Specify text color

Useful Built-in zsh Features

Globbing

zsh's globbing is significantly more powerful than bash's.

# Recursive globbing (includes subdirectories)
ls **/*.md

# Glob Qualifiers
ls *(.)     # Files only
ls *(/)     # Directories only
ls *(om)    # Sort by modification time
ls *(.mh-1) # Files changed within the last hour

Directory Stack

setopt AUTO_PUSHD           # Automatically pushd on cd
setopt PUSHD_IGNORE_DUPS    # Ignore duplicates

# How to use
cd /usr/local    # Added to stack
cd /etc          # Added to stack
dirs -v          # List the stack
cd ~2            # Move to the 2nd item in the stack

Command Line Editing

# Emacs mode (default)
bindkey -e

# Vi mode
bindkey -v

# Adding useful keybindings
bindkey '^R' history-incremental-search-backward  # Search history with Ctrl+R
bindkey '^A' beginning-of-line                     # Go to the start of the line with Ctrl+A
bindkey '^E' end-of-line                           # Go to the end of the line with Ctrl+E

Frameworks and Plugin Managers

zsh's ecosystem includes powerful frameworks and plugin managers.

Oh My Zsh

The most famous zsh framework, developed by Robby Russell in 2009. It features a wide variety of themes and plugins.

# Installation
sh -c "$(curl -fsSL https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"

# Example settings in .zshrc
plugins=(git docker node npm zsh-autosuggestions zsh-syntax-highlighting)
ZSH_THEME="robbyrussell"

Other Options

Tool Features
Prezto More lightweight than Oh My Zsh
zinit High-speed plugin manager
sheldon Written in Rust, fast
Starship Cross-shell compatible prompt

Migration Points: From bash to zsh

Key points for macOS users transitioning from bash to zsh:

# Migrate contents of .bashrc / .bash_profile to .zshrc

# bash settings
export PATH="$HOME/bin:$PATH"        # → Works as is
alias ll='ls -la'                     # → Works as is
source ~/.bash_completion             # → Replace with compinit

# Points to note
# bash: Arrays are 0-indexed
# zsh:  Arrays are 1-indexed (by default)

# If compatibility is required
setopt KSH_ARRAYS  # Change to 0-indexed

Summary

Item Content
Origin of zsh From the login ID of Zhong Shao (1990, Princeton University)
Meaning of rc "Run Commands" (Originates from 1965's CTSS)
macOS Default Since 2019 (macOS Catalina)
Greatest strengths Powerful completion, glob expansion, customizability

zsh is a shell with over 30 years of history that continues to evolve today. By knowing the origin of its name and understanding the meaning of its configuration files, you will be able to master it more deeply.

Be sure to customize your .zshrc to your liking and streamline your daily terminal work.

GitHubで編集を提案

Discussion