Home Terminal Tuts ZSH for beginners: Exploring Linux’s elegant shell

ZSH for beginners: Exploring Linux’s elegant shell

ZSH, a powerful and customizable shell, has won the hearts of many Linux enthusiasts. Our beginner's guide elucidates its basics, making your command-line experience a breeze.

by Arun Kumar
mastering the basics of zsh

Navigating the intricacies of the command line can be an exciting experience for many tech enthusiasts. ZSH (Z Shell) is a shell that stands out for its unique blend of tradition and innovation. In this guide, we will explore the foundational commands of ZSH, delve into its enhanced file operations, and discover how it can be customized to suit your needs.

We will also explore its scripting capabilities, nuanced job control, and many extended features and modules. Additionally,  we will compare ZSH with its venerable counterpart, Bash, and highlight the unique strengths and offerings of each. Whether you are a beginner or an experienced user, this guide will take you on a deep dive into the fascinating world of ZSH.

1. What is ZSH?

In simple terms, ZSH (Z Shell) is a Unix shell, much like the popular bash or fish. The shell acts as an interpreter, allowing users to type commands and watch them come to life. Think of it as a magical realm where you type spells and get results. ZSH, in particular, has a reputation for its interactive features, customization, and themes.

But what truly sets ZSH apart? For me, it’s the myriad of delightful plugins and themes, especially when paired with the oh-my-zsh framework. And not to forget, those auto-suggestions that often make me feel like ZSH can read my mind!

2. Basic ZSH commands: Getting the hang of it

Diving into a new shell might seem daunting, but the basics are pretty straightforward. Let’s look at some of the essential commands to get you started.

2.1. Navigating the filesystem

Command: cd <directory-name>

This is your ticket to move around directories. Just replace <directory-name> with the name of the directory you want to navigate to.

Sample Output:

$ cd Documents/
$ 

Personal note: As much as I love cd, ZSH provides even quicker navigation methods, which we’ll explore later.

2.2. Listing files and directories

Command: ls <options>

Display the list of files and directories in your current location. With different options, you can see them in detail.

Sample Output:

$ ls
file1.txt  file2.txt  directory1  directory2

Personally, I’m not a fan of bland outputs. I always pair ls with -l to get a detailed list!

2.3. Create a new directory

Command: mkdir <directory-name>

This lets you create a new directory. Easy peasy!

Sample Output:

$ mkdir newFolder
$ ls
file1.txt  file2.txt  directory1  directory2  newFolder

2.4. Removing a file

Command: rm <filename>

You can remove any file with this command. But remember, with great power comes great responsibility!

Sample Output:

$ rm file1.txt
$ ls
file2.txt  directory1  directory2  newFolder

Personal note: I’ve accidentally deleted crucial files before (oops!), so I always double-check before hitting enter.

3. Making ZSH even better: Plugins and themes

ZSH alone is powerful, but when combined with the oh-my-zsh framework, it becomes a force to be reckoned with. Here, I’ll introduce you to some of my favorite aspects of this combination.

3.1. Installing oh-my-zsh

Before diving into plugins and themes, you need to install the framework. Use this command:

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

3.2. Changing themes

With oh-my-zsh, you can change themes effortlessly. My personal favorite? The “agnoster” theme! To set it, simply update the ZSH_THEME variable in your .zshrc file.

Personal liking: The “agnoster” theme has a sleek appearance that gives my terminal a professional look.

3.3. Leveraging plugins

Oh-my-zsh comes with a plethora of plugins. For starters, try the zsh-autosuggestions plugin. It suggests commands as you type based on your history, making you feel like a terminal wizard!

To install:

$ git clone https://github.com/zsh-users/zsh-autosuggestions ~/.oh-my-zsh/custom/plugins/zsh-autosuggestions

Then, add zsh-autosuggestions to the list of plugins in your .zshrc.

4. Mastering File Operations with ZSH

ZSH doesn’t just stop with basic commands. It offers numerous enhancements over traditional shells that make file operations easier.

4.1. Globbing: A more advanced way to match files

ZSH provides advanced globbing features, enabling you to match complex patterns easily.

Command: ls **/*.<file-extension>

This recursively lists all files with the specified extension.

Sample Output:

$ ls **/*.txt
dir1/fileA.txt  dir2/fileB.txt

4.2. Using aliases for faster operations

Aliases let you shorten a command or a group of commands.

To create an alias:

alias l='ls -la'

Now, just type l to get a detailed list of files.

Sample Output:

$ l
total 16
drwxr-xr-x  4 user  user  128 Sep 28 15:32 .
drwxr-xr-x  6 user  user  192 Sep 28 15:25 ..
-rw-r--r--  1 user  user    0 Sep 28 15:32 file1.txt

My take: Aliases are a lifesaver! I’ve got a bunch set up for my frequently used commands.


5. Customizing your ZSH prompt

Your terminal shouldn’t be boring. With ZSH, you can make your terminal reflect your personality.

5.1. Changing the prompt

Modify your prompt by changing the PROMPT variable in .zshrc.

Example:

PROMPT='%n@%m %~ %# '

This changes the prompt to show username@hostname followed by the current directory.

5.2. Adding colors

ZSH supports colors in the prompt. Here’s how you can add a touch of vibrancy:

PROMPT='%F{red}%n@%m %~ %# %f'

This will display the username and hostname in red.

My note: I adore adding colors to my terminal. It’s aesthetically pleasing and helps distinguish different parts of the prompt.


6. History and Auto-suggestions

ZSH keeps a record of the commands you’ve executed, making it easier to re-run them.

6.1. Browsing command history

Use the arrow keys to scroll through previously entered commands.

6.2. Searching through command history

Press Ctrl + R and start typing to search through your command history.

Sample Output:

(reverse-i-search)`ls': ls -la

7. Handling scripting and functions

One of ZSH’s strengths is its scripting prowess. The shell isn’t just about navigating files; it’s also a powerful scripting language.

7.1. Creating a basic ZSH script

Save the following in a file named myscript.zsh:

#!/usr/bin/env zsh

echo "Hello from a ZSH script!"

To run the script:

$ chmod +x myscript.zsh
$ ./myscript.zsh

Sample Output:

Hello from a ZSH script!

7.2. Using functions

In ZSH, you can define functions to automate repetitive tasks.

Example function to greet:

greet() {
   echo "Hello, $1!"
}

Usage:

$ greet Alice

Sample Output:

Hello, Alice!

8. Job Control in ZSH

ZSH allows you to control tasks seamlessly.

8.1. Background and foreground jobs

To move a running process to the background:

$ <command> &

To bring it back to the foreground:

$ fg

8.2. Listing and killing jobs

To list current jobs:

$ jobs

To kill a job:

$ kill %1

Here, %1 refers to the job number.


9. Extended features and modules

ZSH is modular, and several built-in modules provide extended features.

9.1. Using the zftp module

Load the module:

$ zmodload zsh/zftp

To connect to an FTP server:

$ ftp_open ftp.example.com

9.2. Using math functions

Load the module:

$ zmodload zsh/mathfunc

Example:

$ echo $((sin(1)))

Sample Output:

0.8414709848079

10. ZSH vs. Bash: Battle of the Shells

For many, the terminal’s heart revolves around two dominant shells: ZSH and Bash. Both have their own legion of followers and a rich array of features. But how do they stack up against each other?

10.1. History and popularity

  • Bash (Bourne Again SHell): Bash is the successor to the original Unix shell. It’s been around since 1989 and, due to its longevity, has become the default shell for many Linux distributions and macOS (until Catalina).
  • ZSH (Z Shell): ZSH, while younger than Bash, has been around since 1990. It has gained significant traction in recent years, especially with the oh-my-zsh framework and Apple making it the default shell in macOS Catalina.

10.2. Scripting capabilities

  • Bash: Bash offers robust scripting capabilities. Many scripts written for the Bourne shell (sh) can be executed in Bash without modification.
  • ZSH: ZSH provides advanced scripting features, with improvements over Bash in areas like associative arrays and floating-point arithmetic.

10.3. Interactive features

  • Bash: While Bash is powerful, its interactive features are somewhat limited compared to ZSH. Bash 4.0+ introduced features like associative arrays and improvements to its array handling, but its interactive capabilities are still considered basic.
  • ZSH: ZSH shines when it comes to interactive use. Features like spell correction, path replacement, and its extended globbing make it user-friendly and efficient.

10.4. Customization and themes

  • Bash: Bash does allow customization, and with tools like Bash-it, users can add themes and plugins. But the options are relatively limited.
  • ZSH: With frameworks like oh-my-zsh and prezto, ZSH offers a wide range of themes, plugins, and customization options. This is where ZSH arguably outshines Bash.

10.5. Compatibility

  • Bash: Given its age and widespread adoption, Bash scripts are ubiquitous. Bash ensures a high level of compatibility with these scripts.
  • ZSH: While ZSH can run most Bash scripts without modification, there might be occasional incompatibilities. However, for day-to-day operations, this is rarely a concern.

Note: While I’ve come to love ZSH for its interactive features and customization options, I can’t deny the foundational role Bash has played in shaping the Unix world. Both shells have their merits. The choice between them often boils down to personal preference and the specific needs of the user.

ZSH quick reference table

 

Command Description
cd <dir> Change to directory <dir>
ls List files in the current directory
pwd Display the current directory path
echo <text> Display <text> to the terminal
source <file> Execute commands from a file in the current shell
alias l='ls -la' Create an alias l for the command ls -la
history Show command history
bindkey -v Enable VI mode for command-line editing
setopt Set ZSH options
autoload Mark a function for autoloading

Conclusion

Diving into the world of ZSH, we explored its features, ranging from basic to advanced commands, customization options, scripting capabilities, and its interactive prowess. ZSH offers many enhancements over traditional shells, particularly in terms of globbing, aliases, history management, job control, and extended modules. By comparing ZSH with Bash, we can navigate the strengths and unique offerings of each shell, but ZSH stands out for its adaptability, making it a favorable choice for those seeking a blend of tradition and innovation in their terminal experiences.

You may also like

Leave a Comment

fl_logo_v3_footer

ENHANCE YOUR LINUX EXPERIENCE.



FOSS Linux is a leading resource for Linux enthusiasts and professionals alike. With a focus on providing the best Linux tutorials, open-source apps, news, and reviews written by team of expert authors. FOSS Linux is the go-to source for all things Linux.

Whether you’re a beginner or an experienced user, FOSS Linux has something for everyone.

Follow Us

Subscribe

©2016-2023 FOSS LINUX

A PART OF VIBRANT LEAF MEDIA COMPANY.

ALL RIGHTS RESERVED.

“Linux” is the registered trademark by Linus Torvalds in the U.S. and other countries.