We compare the Bash, Zsh, and fish shells
Functions
All shells let you combine several commands into one function. As Listing 2 shows, Bash and Zsh use an identical syntax, whereas fish again goes its own way.
Listing 2
Functions
# Bash and Zsh saywhat() { echo "Hello!" } # Bash and Zsh (alternatively) function saywhat { echo "Hello!" } # Fish function saywhat echo "Hello!" end
Access to a variable only works in the context in which you defined it. If you use Bash or Zsh, only those variables that you explicitly mark as local
are local:
local name = "Peter
Zsh also supports anonymous functions that have no name. Such a function is immediately executed by the shell as soon as it stumbles upon its definition in the code. This can be used, for example, to restrict the visibility of variables in start scripts to the body of the function.
Outsourced
Zsh and fish can automatically reload functions used by a script from separate files. However, this autoload mechanism differs slightly in the two shells.
For example, in Zsh you first need to explicitly register the corresponding functions with the autoload
keyword. This shell also lets you extend the range of functions using modules, which you can even load at runtime if required. Some of these are already provided by the shell: For example, the zsh/zftp
module adds a full FTP client.
Using the built-in compile
command, Zsh can compile functions and scripts and store them in a binary file. The results are loaded faster then, but they are not available in other shells.
Configuring Zsh
The best way to try out fish is through the online browser interface [6] or by running the fish_config
utility and exploring the graphic configuration utility. With Zsh, you'll also want to install it and experiment before deciding to migrate.
Zsh has one thing Bash doesn't have: a handy configuration menu that appears when you start the shell for the first time. Zsh looks for the configuration files, and if it can't find them (in other words, if you haven't configured the shell yet), the config menu launches (Figure 4). Press 1
to enter the main menu (Figure 5).
When you get to the main menu, press 1
to set the scope of the history. You can enter the number of lines of history you would like to retain within the shell, select a file for saving the history, and choose how many lines you would like to keep within the file. In the example shown in Figure 6, the history has been extended to 10,000 lines.
Enter
to save and return to the main window, or enter q
to return without saving.
Back in the main window, press 2
to go to the autocomplete settings. You can choose between the default autocomplete options (1
) or run a configuration tool that will let you customize the autocomplete settings (2
). Choose 3
if you don't want to use autocompletion.
Item 3
in the main menu lets you set the keyboard behavior in the shell's line editor. You can get the editor to behave like either of the classic Unix editors Emacs or vi. It is best to leave the suggested value.
Item 4
in the main menu lets you configure some other common shell settings (Figure 7), including settings related to pattern matching, command history, and whether to beep on errors.
When you're finished with the configuration menu, you can save the settings by pressing
in the main window. The new settings will take effect immediately.
An example of a Zsh feature with lots of room to customize is the command prompt. The developers have invested considerable energy into command prompt options. Customizing the string of text that appears before you enter a command might seem like a random personal choice, but many admins use the prompt for orientation and documentation purposes. For instance, including the date and time on a prompt means that a screenshot will document exactly when the command was run.
Use the following commands in Zsh to load the preconfigured prompt options:
autoload -U promptinit promptinit
After you enter these commands, you can list the available prompt themes with prompt -l
. To see what they all look like, enter prompt -p
(Figure 8).
Note that the two-line prompts in Figure 8, which show the prompt above the actual command line, add clarity for situations where the prompt is providing essential information for the user. After you set the prompt interactively with:
prompt -s pattern_name
a hint appears telling you that, for a permanent change of the prompt, you have to enter the pattern in ~/.zshrc
. The entry in .zshrc
is similar to what you just did at the command line (Listing 3).
Listing 3
Changing the Prompt
autoload -Uz promptinit promptinit prompt pattern_name
The Zsh configuration files are shown in Table 1. The names in Table 1 refer to the files in the user's home directory. Global files with the same names (but without the leading dot) are found in /etc
.
Table 1
Zsh Configuration Files
File Name | Description |
---|---|
.zprofile |
Login settings and commands |
.zshenv |
Settings that apply regardless of how the shell is called |
.zshrc |
Shell startup settings and commands |
.zlogin |
Commands for logging in |
.zlogout |
Commands for logging out of a session |
Zsh processes the configuration files in the order .zprofile
, .zshrc
, and .zlogin
when you log in. Use the .zshenv
file to configure settings that apply regardless of how the shell was started. When called manually, Zsh only evaluates .zshrc
and .zshenv
. When logging out of the session, the commands from .zlogout
are executed.
Note that the .zlogin
and .zlogout
config files make it possible to facilitate tasks by creating specially tailored user accounts, for example, for data backup, batch jobs in remote data transmission, or system maintenance. In this way, you can even delegate tasks to nonexperts, because all they have to do is log on, and the task executes automatically. Placing exit
in the startup file automatically logs the user off after the task is completed.
If you don't like the predefined prompt themes provided with Zsh, you can design your own prompt. Like other personal settings for interactive sessions, prompt design details go in .zshrc
. Setting up your own prompt basically works like it does in Bash, and even the escape sequences for the colors are the same [4], but you need to initiate them in Zsh using \e
. The placeholders you use to build the prompt content, on the other hand, are completely different from those for Bash (see Table 2). For more information, refer to the Zsh manual [2].
Table 2
Zsh Prompt
Meaning | Statement |
---|---|
Date |
%D |
Time |
%T |
Host name without domain |
%m |
Full computer name |
%M |
User ID |
%n |
Exit code of last command |
%? |
Current directory |
%/ |
Current directory |
%~ (truncates the home directory specification) |
Sequence number in history |
%! |
Variables and aliases are entered in the .zshenv
file. In our test, the usual colors for directories, executables, and so on were not output by the ls
command; I corrected this with the following line:
alias ls="ls --color"
Any additional variables and $PATH
extensions work like in Bash and need to be added to .zshenv
.
Buy this article as PDF
(incl. VAT)
Buy Linux Magazine
Direct Download
Read full article as PDF:
Price $2.95
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters
Find SysAdmin Jobs
News
-
CarbonOS: A New Linux Distro with a Focus on User Experience
CarbonOS is a brand new, built-from-scratch Linux distribution that uses the Gnome desktop and has a special feature that makes it appealing to all types of users.
-
Kubuntu Focus Announces XE Gen 2 Linux Laptop
Another Kubuntu-based laptop has arrived to be your next ultra-portable powerhouse with a Linux heart.
-
MNT Seeks Financial Backing for New Seven-Inch Linux Laptop
MNT Pocket Reform is a tiny laptop that is modular, upgradable, recyclable, reusable, and ships with Debian Linux.
-
Ubuntu Flatpak Remix Adds Flatpak Support Preinstalled
If you're looking for a version of Ubuntu that includes Flatpak support out of the box, there's one clear option.
-
Gnome 44 Release Candidate Now Available
The Gnome 44 release candidate has officially arrived and adds a few changes into the mix.
-
Flathub Vying to Become the Standard Linux App Store
If the Flathub team has any say in the matter, their product will become the default tool for installing Linux apps in 2023.
-
Debian 12 to Ship with KDE Plasma 5.27
The Debian development team has shifted to the latest version of KDE for their testing branch.
-
Planet Computers Launches ARM-based Linux Desktop PCs
The firm that originally released a line of mobile keyboards has taken a different direction and has developed a new line of out-of-the-box mini Linux desktop computers.
-
Ubuntu No Longer Shipping with Flatpak
In a move that probably won’t come as a shock to many, Ubuntu and all of its official spins will no longer ship with Flatpak installed.
-
openSUSE Leap 15.5 Beta Now Available
The final version of the Leap 15 series of openSUSE is available for beta testing and offers only new software versions.