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
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters
Support Our Work
Linux Magazine content is made possible with support from readers like you. Please consider contributing when you've found an article to be beneficial.
News
-
Fedora 39 Beta is Now Available for Testing
For fans and users of Fedora Linux, the first beta of release 39 is now available, which is a minor upgrade but does include GNOME 45.
-
Fedora Linux 40 to Drop X11 for KDE Plasma
When Fedora 40 arrives in 2024, there will be a few big changes coming, especially for the KDE Plasma option.
-
Real-Time Ubuntu Available in AWS Marketplace
Anyone looking for a Linux distribution for real-time processing could do a whole lot worse than Real-Time Ubuntu.
-
KSMBD Finally Reaches a Stable State
For those who've been looking forward to the first release of KSMBD, after two years it's no longer considered experimental.
-
Nitrux 3.0.0 Has Been Released
The latest version of Nitrux brings plenty of innovation and fresh apps to the table.
-
Linux From Scratch 12.0 Now Available
If you're looking to roll your own Linux distribution, the latest version of Linux From Scratch is now available with plenty of updates.
-
Linux Kernel 6.5 Has Been Released
The newest Linux kernel, version 6.5, now includes initial support for two very exciting features.
-
UbuntuDDE 23.04 Now Available
A new version of the UbuntuDDE remix has finally arrived with all the updates from the Deepin desktop and everything that comes with the Ubuntu 23.04 base.
-
Star Labs Reveals a New Surface-Like Linux Tablet
If you've ever wanted a tablet that rivals the MS Surface, you're in luck as Star Labs has created such a device.
-
SUSE Going Private (Again)
The company behind SUSE Linux Enterprise, Rancher, and NeuVector recently announced that Marcel LUX III SARL (Marcel), its majority shareholder, intends to delist it from the Frankfurt Stock Exchange by way of a merger.