Zsh is a powerful command-line shell for operating systems based on Unix that is renowned for its customization options and wide range of features. This cheat sheet is a must-read if you’re new to Zsh or want to brush up on your knowledge. To help you work in the terminal more effectively and efficiently, I’ll go over some of the most useful Zsh commands and shortcuts in this article.
Download Zsh Cheat Sheet
What is Zsh?
Zsh, which stands for Z Shell, is a command-line shell for Unix-like operating systems. It is an enhanced version of the Bourne shell (sh) with new features and improvements. Zsh provides enhanced customization, enhanced command-line editing capabilities, and advanced scripting features.
Furthermore, Zsh provides an interactive and user-friendly experience, allowing users to efficiently navigate and edit commands with features such as history search and customizable key bindings. Its extensive customization options allow users to personalize their shell environment for a tailored workflow, including prompt styles, aliases, and configuration settings.
So, it is clear that Zsh is a popular choice for automation and complex scripting tasks due to its advanced scripting capabilities, which support programming constructs and functions.
Why Use Zsh Instead of Bash?
There are several reasons why someone might prefer Zsh over Bash. To begin with, Zsh has additional features and improvements than Bash, such as advanced autocompletion, improved command-line editing capabilities, and more powerful scripting options. These enhancements have the potential to increase productivity and streamline workflow.
Secondly, it offers more customization options than Bash, allowing users to personalize their shell environment to a greater extent. Thirdly, Zsh’s autocompletion mechanism is extremely sophisticated, suggesting completions not only for commands and filenames, but also for options, variables, and other variables. Furthermore, Zsh was created with modern features and extensibility in mind, making it adaptable to changing needs.
Zsh Commands List
Zsh is a powerful shell with a plethora of commands and features. To help you navigate the world of Zsh more easily, I’ve categorized some of the most useful commands by their function in this section.
Installing Zsh Commands
In this section, you can find commands to install Zsh on different Linux distributions.
Commands |
Description |
sudo dnf install zsh |
Installs Zsh with user’s package manager on Fedora, CentOs and RHEL |
sudo apt install zsh |
Installs Zsh with user’s package manager on Debian and Ubuntu |
sudo port install zsh |
Installs Zsh with user’s package manager on MacOS and user can install it using MacPorts |
brew install zsh |
Installs Zsh with user’s package manager on MacOS and user can install it using Homebrew |
Navigation Commands
Here, I have listed some of the most useful navigation commands. Using these commands, you will be able to navigate through the directories and files.
Commands |
Description |
cd |
Changes the current directory |
cd .. |
Moves up one level in the directory hierarchy |
cd / |
Changes the current directory to the root directory |
cd ~ |
Changes the current directory to your home directory |
cd -P |
Changes the current directory and resolves symbolic links |
cd -L |
Changes the current directory without resolving symbolic links |
cd /path/to/directory |
Changes the current directory to the specified directory path |
code . |
Opens the current directory in Visual Studio Code |
dirs |
Displays or manipulates the directory stack |
explorer . |
Opens the current directory in the default file explorer (on Windows) |
ls |
Lists files and directories in the current directory |
open . |
Opens the current directory in the default file explorer (on macOS) |
pushd |
Changes the current directory and pushes the current directory onto the directory stack |
popd |
Removes entries from the directory stack and changes the current directory to the popped directory |
pwd |
Shows the current working directory |
Control Flow and Script Execution Commands
Whether you’re writing a complex script or just trying to automate a simple task, knowing control flow and script execution is essential. In this section, I’ll go over some of the most important commands in Zsh for controlling flow and script execution.
Commands |
Description |
break |
Exits from a loop or switch statement |
bye |
Exits the shell |
continue |
Skips the remaining iterations of a loop and starts the next iteration |
return |
Exits a function with an optional return value |
exit |
Exits the shell with an optional exit status |
fc |
Manages and edits command history |
shift |
Shifts positional parameters to the left by a specified number |
test |
Evaluates conditional expressions and performs tests |
trap |
Sets or displays actions to be taken when a signal is received |
Process Control Commands
In a terminal environment, you frequently need to manage multiple jobs and processes at the same time. Within the shell, you can use the following commands to create, manage, and control jobs and processes. You can pause a job, bring it to the foreground, or send it to the background.
Commands |
Description |
bg [job …] |
Resumes suspended jobs in the background |
disown [job …] |
Removes jobs from the job table, allowing them to continue running |
fg [job …] |
Brings jobs to the foreground |
jobs |
Lists active jobs and their statuses |
kill [options] job … |
Sends signals to processes or job IDs |
suspend [-f] |
Suspends the current shell session |
wait [job …] |
Waits for background jobs to complete |
Variable and Environment Management Commands
In this section, I’ll cover some of Zsh’s most crucial commands for managing variables and the environment, which you can use to define variables, modify the environment, and manage how your scripts and commands communicate with the shell and other programs.
Commands |
Description |
declare |
Displays or sets shell variables |
export |
Sets environment variables |
readonly |
Marks variables as read-only |
typeset |
Declares variables with specific attributes |
unset |
Unsets variables or functions |
unsetopt |
Disables shell options |
Conditional Execution and Scripting Commands
Understanding conditional execution in the world of zsh scripting is essential for managing the flow of your script. You will learn how to use these strong commands in this section, giving you the knowledge necessary to create scripts that are effective and adaptable.
Commands |
Description |
if |
Executes a block of code based on a condition, If the condition is true, the code within the “if” block is executed; otherwise, it is skipped |
then |
Specifies the start of the code block to be executed if the condition of an “if” statement is true |
else |
Specifies the code block to be executed if the condition of the preceding “if” statement is false |
fi |
Marks the end of an “if” statement |
do |
Specifies the start of the code block to be executed in a “for” or “while” loop |
done |
Marks the end of a “for” or “while” loop |
for |
Executes a block of code repeatedly for a specified number of iterations or for each item in a list |
until |
Executes a block of code repeatedly until a certain condition is true |
while |
Executes a block of code repeatedly as long as a certain condition is true |
Command Execution and Environment
Here you’ll find commands to create scripts that loop through data, make decisions based on conditions, and execute commands based on those decisions.Understanding these commands is essential for effective scripting with Zsh, whether you’re writing a simple script or a complex automation tool.
Commands |
Description |
noglob |
Executes a command without performing globbing on arguments |
rehash |
Updates the internal hash table of executable commands |
source |
Executes commands from a file in the current shell session |
time |
Displays the system and user times for the current shell session |
Command Information and Location
You’ll probably need to find information about the commands you’re using as you work with Zsh, such as their location and how they’re being executed. You can use the commands below to find the location of executables, observe how commands are executed, and manage the command hash table.
Commands |
Description |
unhash |
Removes commands from the internal hash table |
where |
Displays all locations where a command is defined |
which |
Displays the path to the executable file of a command |
whence |
Displays information about a command, including its type and location |
History and Command Editing
Frequently, you must recall and modify previously executed commands. Here, I’ll cover history and command editing commands, which allow you to edit and re-execute previous commands as well as parse command line arguments, making it easier to create complex scripts and automation tools.
Commands |
Description |
fc |
Views, edits, re-executes, or changes the order of previously entered commands |
getln |
Displays the command history, showing a list of previously executed commands along with their line numbers |
getopts optstring |
Allows to process command-line arguments and options, and enables to create more interactive and flexible shell scripts |
history |
Displays the command history, showing a list of previously executed commands along with their line numbers |
Input/Output Commands
In this section, I am going to cover those commands that you will need to read input from the user and customize the behavior of your shell.
Commands |
Description |
echo |
Displays a line of text or the value of a variable on the terminal |
print |
Displays text or variables on the terminal, and various formatting options and can be used to control the output appearance |
read |
Reads input from the user or from a file and assigns it to variables and allows user to prompt for input, store the entered values, and perform further processing based on the input |
ttyctl -fu |
Controls terminal settings and behavior, and -fu options force a flush of the terminal output buffer, ensuring that all pending output is displayed immediately |
Logging and Shell Exit Commands
Keeping track of your terminal activity is an essential part of command-line work. You’ll learn commands in this section that will allow you to log your terminal activity to a file, exit your shell session, and temporarily pause the execution of your current process.
Commands |
Description |
log |
Records shell input/output to a file |
logout |
Exits the current shell session with an optional exit status |
Shell Built-in Commands
In this section, I’ve listed commands that are essential to the functionality of the shell and are also known as shell-built in commands. You can define and manage aliases for frequently used commands, customize shell behavior, manipulate variables and functions, enable and disable shell options, and much more with these commands.
Commands |
Description |
alias |
Defines or lists aliases for commands |
unalias |
Removes aliases for commands |
builtin |
Executes a built-in shell command |
disable |
Disables shell built-in commands or functions. |
enable |
Enables shell built-in commands or functions |
false |
Returns a non-zero exit status |
functions |
Lists or defines functions |
unfunction |
Removes defined shell functions |
hash |
Displays or modifies command hash table |
set |
Sets or displays shell options or positional parameters |
setopt |
Sets or displays shell options |
true |
Returns a zero exit status |
type |
Displays information about a command |
typeset |
Declares or displays shell variables with additional attributes |
unset |
Unsets shell variables or functions |
unsetopt |
Unsets shell options |
File and Directory Management Commands
Zsh provides a set of powerful commands that allow you to easily create, manipulate, and remove files and directories; a list of some of these commands is below.
Commands |
Description |
cp |
Creates copies of files and directories |
file |
Displays information about the specified file(s) |
mkdir |
Creates a new directory |
mkcd |
Creates a new directory and changes the current directory to it in a single command |
mv |
Moves or renames files and directories |
rm |
Removes a file |
rm -r |
Removes a directory and its contents recursively |
touch |
Creates an empty file or updates the access and modification timestamps of an existing file |
zmv |
Allows users to rename multiple files using complex patterns and expressions. |
zmvn |
Allows users to files rename using natural sorting order, which handles numerical sequences in a more human-friendly way |
Loading and Unloading Modules Commands
Zsh is a highly customizable shell that allows you to load and unload modules, allowing you to extend its functionality and tailor it to your specific needs. In this section, I’ll look at module loading and unloading in Zsh, as well as various commands and options.
Commands |
Description |
zmodload -dL |
Loads one or more shared modules into the Zsh shell |
zmodload -e |
Loads one or more modules and generates error messages if loading fails |
zmodload [ -a [ -bcp [ -I ] ] ] [ -iL ] … |
Loads modules, specifying various loading options |
zmodload-u [ -abcdp [ -I ] ] [ -iL ] … |
Unloads modules, specifying various unloading options |
zmodload -d [ -L ] [ name ] |
Unloads a specific module or all loaded modules |
zmodload -d name dep |
Unloads a module and its dependencies |
zmodload -ud name [ dep … ] |
Unloads a module and its dependencies and unloads any unused modules |
zmodload -ab [ -L ] |
Lists all available built-in modules |
zmodload -ab [ -i ] name [ builtin … ] |
Loads one or more built-in modules and generates error messages if loading fails |
zmodload -ub [ -i ] builtin … |
Unloads one or more built-in modules |
zmodload -ac [ -IL ] |
Lists all available conditional modules |
zmodload -ac [ -iI ] name [ cond … ] |
Loads one or more conditional modules and generates error messages if loading fails |
zmodload -uc [ -iI ] cond … |
Unloads one or more conditional modules |
zmodload -ap [ -L ] |
Lists all available parameter expansion modules |
zmodload -ap [ -i ] name [ parameter … ] |
Loads one or more parameter expansion modules and generates error messages if loading fails |
zmodload -up [ -i ] parameter … |
Unloads one or more parameter expansion modules |
zmodload -a [ -L ] |
Lists all available modules |
zmodload -a [ -i ] name [ builtin … ] |
Loads one or more modules and generates error messages if loading fails |
zmodload -ua [ -i ] builtin … |
Unloads one or more modules |
zmodload -e [ string … ] |
Evaluates the given strings as Zsh code |
Miscellaneous
As a powerful shell, Zsh includes a number of commands that add functionality and customization options. The commands listed below allow you to perform mathematical calculations directly in the shell, simplify command-line arguments, help customize various aspects of Zsh’s behavior, and many more.
Commands |
Description |
zcalc |
Performs arithmetic calculations directly in the shell |
xargs |
Allows users to apply another command to a list of arguments, similar to xargs but with enhanced features |
zprof |
Provides profiling information for Zsh startup files, allowing user to optimize their loading time |
zstyle |
Customizes the behavior and appearance of the shell by setting various options and styles |
zargs |
Applies another command to a list of arguments |
zprof |
Profiles Zsh startup files, helps to optimize their loading time by identifying potential performance bottlenecks |
zstyle |
Configures and customizes various aspects of Zsh’s behavior, including shell options, command completion, and highlighting |
vared |
Provides a convenient way to edit variables in the shell using the editor specified by the EDITOR environment variable |
push-line |
Pushes the current command line onto the buffer and allows you to continue editing it, useful for complex command constructions |
autoload |
Allows users to lazily load Zsh functions |
clone/detach |
Allow you to create new instances of the current shell, either as a forked child process (clone) or as a detached background process (detach) |
zcompile |
Compiles Zsh scripts into a more efficient format, which can improve their execution time |
zpty |
Provides a way to create and interact with pseudo-terminals from within Zsh |
ztcp |
Allows users to create TCP and UDP connections directly from the shell. It can be used for various network-related tasks |
zsocket |
Allows users to create and interact with Unix domain sockets from within Zsh. This can be useful for inter-process communication |
zparseopts |
Simplifies the handling of command-line options and arguments in Zsh scripts by providing a convenient way to parse and process them |
zregexparse |
Provides a way to parse text using regular expressions and extract specific elements of interest. It is particularly useful for text manipulation tasks |
zsh-mime-setup |
Configures default programs and actions associated with different MIME types in the Zsh environment |
Conclusion
To conclude, this Zsh Cheat Sheet is a great resource for learning important Zsh commands. Having these commands at your fingertips will increase your productivity and efficiency in the Zsh shell. Keep this cheat sheet handy to fully utilize Zsh for your command-line tasks. I hope you found this article useful. Please leave a comment if you have any questions or suggestions.
Thank you very much, is clean work neat and well organized, you make it so simple .
good job and thank you again.
You’re welcome. Glad to hear that you found the article useful. Stay connected with us for more contents like this.
Thanks for your compliments.