Zsh-Specific Features
Globbing
1# Recursive globbing
2ls **/*.txt
3
4# Exclude pattern
5ls ^*.txt
6
7# Numeric ranges
8ls file<1-10>.txt
9
10# Qualifiers
11ls *(.) # Files only
12ls *(/) # Directories only
13ls *(.x) # Executable files
14ls *(m-7) # Modified in last 7 days
15ls *(Lm+100) # Larger than 100MB
Arrays
1# Array creation
2arr=(one two three)
3
4# Access elements
5echo $arr[1] # First element (1-indexed in zsh)
6echo $arr[-1] # Last element
7
8# Array slicing
9echo $arr[2,3]
10
11# Array length
12echo $#arr
Parameter Expansion
1# Default value
2echo ${VAR:-default}
3
4# Assign default
5echo ${VAR:=default}
6
7# Substring
8str="hello world"
9echo ${str:0:5} # "hello"
10
11# Remove pattern
12path="/usr/local/bin"
13echo ${path#*/} # Remove shortest match from start
14echo ${path##*/} # Remove longest match from start
15echo ${path%/*} # Remove shortest match from end
16echo ${path%%/*} # Remove longest match from end
17
18# Replace
19echo ${str/world/universe}
Completions
1# Enable completions
2autoload -Uz compinit && compinit
3
4# Case-insensitive completion
5zstyle ':completion:*' matcher-list 'm:{a-z}={A-Za-z}'
6
7# Menu selection
8zstyle ':completion:*' menu select
9
10# Rehash for new commands
11rehash
History
1# History search
2Ctrl+R # Reverse search
3
4# History expansion
5!! # Last command
6!$ # Last argument
7!^ # First argument
8!* # All arguments
9!-2 # 2nd last command
10
11# Search history
12history | grep pattern
13
14# Execute from history
15!123 # Execute command #123
Bourne Shell (sh) Compatible
POSIX Scripting
1#!/bin/sh
2
3# Variables (no spaces around =)
4VAR="value"
5
6# Command substitution
7TODAY=$(date +%Y-%m-%d)
8TODAY=`date +%Y-%m-%d` # Old style
9
10# Conditionals
11if [ -f "file.txt" ]; then
12 echo "File exists"
13elif [ -d "dir" ]; then
14 echo "Directory exists"
15else
16 echo "Not found"
17fi
18
19# String tests
20if [ "$VAR" = "value" ]; then
21 echo "Match"
22fi
23
24if [ -z "$VAR" ]; then
25 echo "Empty"
26fi
27
28if [ -n "$VAR" ]; then
29 echo "Not empty"
30fi
31
32# Numeric tests
33if [ "$NUM" -gt 10 ]; then
34 echo "Greater than 10"
35fi
36
37# File tests
38[ -e file ] # Exists
39[ -f file ] # Regular file
40[ -d dir ] # Directory
41[ -r file ] # Readable
42[ -w file ] # Writable
43[ -x file ] # Executable
Loops
1# For loop
2for file in *.txt; do
3 echo "$file"
4done
5
6# While loop
7while read line; do
8 echo "$line"
9done < file.txt
10
11# Until loop
12count=0
13until [ $count -eq 5 ]; do
14 echo $count
15 count=$((count + 1))
16done
17
18# C-style (not POSIX)
19i=0
20while [ $i -lt 10 ]; do
21 echo $i
22 i=$((i + 1))
23done
Functions
1# Function definition
2my_function() {
3 echo "Arg 1: $1"
4 echo "Arg 2: $2"
5 return 0
6}
7
8# Call function
9my_function arg1 arg2
10
11# Check return value
12if my_function; then
13 echo "Success"
14fi
Zsh Configuration
.zshrc Essentials
1# Oh My Zsh
2export ZSH="$HOME/.oh-my-zsh"
3ZSH_THEME="robbyrussell"
4plugins=(git docker kubectl)
5source $ZSH/oh-my-zsh.sh
6
7# Aliases
8alias ll='ls -lah'
9alias gs='git status'
10alias gp='git pull'
11
12# Custom prompt
13PROMPT='%F{cyan}%n@%m%f:%F{yellow}%~%f$ '
14
15# History settings
16HISTSIZE=10000
17SAVEHIST=10000
18HISTFILE=~/.zsh_history
19setopt SHARE_HISTORY
20setopt HIST_IGNORE_DUPS
21
22# Key bindings
23bindkey '^[[A' history-search-backward
24bindkey '^[[B' history-search-forward
25
26# Auto-cd
27setopt AUTO_CD
28
29# Correction
30setopt CORRECT
31setopt CORRECT_ALL
Useful Aliases
1# Navigation
2alias ..='cd ..'
3alias ...='cd ../..'
4alias ....='cd ../../..'
5
6# Git
7alias g='git'
8alias ga='git add'
9alias gc='git commit'
10alias gco='git checkout'
11alias gd='git diff'
12alias gl='git log --oneline'
13
14# Docker
15alias d='docker'
16alias dc='docker-compose'
17alias dps='docker ps'
18alias dimg='docker images'
19
20# System
21alias update='sudo apt update && sudo apt upgrade'
22alias ports='netstat -tuln'
23alias myip='curl ifconfig.me'
Further Reading
Related Snippets