A smart and friendly shell
Teach a Man to Fish…
The fish shell provides many features that rival the well-known Bash. We examine some highlights.
Bruce Byfield
Bruce Byfield is a computer journalist and a freelance writer and editor specializing in free and open source software. In addition to his writing projects, he also teaches live and e-learning courses. In his spare time, Bruce writes about Northwest coast art. You can read more of his work at http://brucebyfield.wordpress.com
The home page for the Friendly Interactive SHell (fish) [1] still proclaims it "the command line shell for the 90s." However, with version 2.1.0 released in October 2013, fish remains in active development and includes dozens of small innovations that together make the command line a friendlier place.
Fish is available in the package repositories of many major distributions [2]. If yours does not include it, you can download the code by cloning the GitHub repository with
git clone git://github.com/fish-shell/fish-shell.git
and build it by following the instructions at the bottom of the GitHub page.
Once fish is installed, you can start it by entering the command fish
at a command prompt. If you plan to use fish regularly, consider creating a profile in your virtual terminal that defaults to fish rather than Bash. Another option is to set fish as your default shell with the command ch -s fish
.
Fish uses most of the same commands as Bash [3] and, in some cases, the same syntax as well. However, fish also includes significant differences, so first-timers are well-advised to begin with the online tutorial [4] and the documentation [5], which is opened in your web browser when you run the help command. All of fish's documentation is refreshingly clear, concise, and complete (Figure 1).
Basic Features – With Extras
The first feature you are likely to notice is syntax highlighting. This feature is far more extensive than the highlighting with the command ls --color
, which displays different file types in different colors. Fish's highlighting usually begins with red, which identifies an invalid command. When your typing results in a valid command, the color changes to blue, with paths to source or target directories in light blue and underlined if they are valid.
Another feature that you will notice almost immediately is auto-suggestion. As you type, suggestions for completion appear in gray. These completions can be commands, directory and file paths, programs, and even man pages, options, and variables. You can either ignore the suggestions and continue typing or press Ctrl+F or the Right Arrow key to accept the suggestion.
These auto-suggestions are in addition to the extensive tab completions. As in many shells, you can press the Tab key to have fish list possible completions, and continue pressing Tab until you reach the completion you want. However, fish's tab completions also include brief descriptions of each item on the list, and they include not only commands but any part of the syntax. In other words, you can use tab completion at any point in your typing, not just for the command (Figure 2).
As you continue to use fish, you will discover other advanced features as well. For example, history works just as it does in most shells, by pressing the Up and Down arrows, whereas the unsupplemented command history lists the commands you have entered.
As with other basic shell features, fish adds one or two extras. By default, fish deletes duplicate entries. Additionally, you can search the history and delete items either with the web-based configuration tool or with the history
command, using history -search STRING
to list commands that include the string and history -delete STRING
to remove with confirmations all commands that include the string (Figure 3).
Configuring Fish
Fish uses the Nano text editor for editing configuration files. This choice of editor has the advantage of building on-screen help into the interface and generally requiring less knowledge to begin using, compared with Vim and Emacs.
The editor includes a kill ring (a shell's clipboard) that can store multiple items. Copying to the kill ring depends on the cursor position: Ctrl+W moves the word before the cursor to the kill ring, and Alt-D moves the next word. Similarly, Ctrl+K moves the contents from the cursor to the end of the line to the kill ring, and Ctrl+U moves the contents from the cursor to the start of the line. Ctrl+Y pastes the latest entry in the kill ring into the editor, and you can select an earlier item with meta key+Y.
The standard way to configure fish is to open the files in ~/.config/fish
in the text editor or to create an environment variable using the set
command. Key bindings, as well as functions and variables for scripting, can all be created these ways. With the latest release, you can set several basic features by entering the command fish_config
for the online configuration tool. For example, you can open fish's web-based configuration tool and assign colors, underlining, and a background to up to 17 different parts of the syntax. However, making extensive use of the syntax highlighting generally requires a dark background to avoid making at least some of the choices invisible (Figure 4).
The fish_config
tool includes a tab containing sample prompts (Figure 5). Each prompt is given a name, with the code to create the prompt manually beside it and the output at the top of the tab. The code is entered in ~/.config/fish/functions/fish_prompt
, although it can be created manually at the command prompt; this usually takes longer to set up, but the resulting prompt may be more exactly what you want.
An option not included in fish_config
is the ability to add your own completions for commands. Instead, you can use the complete
command. Each custom completion must be associated with a command and can include a short option of a single letter and a long option of two or more characters. The custom completion can also be associated with a command option and include a description of what the command does. All completions are saved to ~/.config/fish/completions
.
Fish vs. Bash
I've described only some of the highlights of fish – mainly, those most likely to interest everyday users. In particular, fish's scripting features deserve an article by themselves, especially because variables and functions are organized and used differently than in Bash. Most of these differences strike me as more rational than Bash's equivalents, but they do take some getting use to.
In fact, if fish has a flaw, it is that the shell spoils you for Bash and other shells. That is fine if you switch entirely to fish, but if you switch back and forth, fish's differences are elaborate enough and numerous enough that confusion is easy. I would be exaggerating if I said that fish is to Bash what Bash is to the Windows shell; however, especially in configuration, the analogy comes close to being true.
For this or other reasons, you may decide – perhaps reluctantly – that fish is not for you. If you do, use chsh
to change your default shell back to Bash, or you will probably have to edit the current account as root before you can log in to it again. If fish is not installed from a repository, the documentation suggests running these commands to remove it completely:
rm -Rf /usr/local/etc/fish /usr/local/share/fish ~/.config/fish rm /usr/local/share/man/man1/fish*.1 cd /usr/local/bin rm -f fish mimedb fish_pager fishd fish_indent
However, even if you never use fish as your default shell, you might just want to keep it for use in an alternative profile, so you can occasionally enjoy the rich feature set. These days, use of the command line seems to be declining, but fish is an example of how much a shell can still do, given a bit of rethinking and determination.
Infos
- Fish: http://fishshell.com/
- Fish download: http://fishshell.com/files/2.1.0/linux/index.html
- Fish commands: http://fishshell.com/docs/current/commands.html
- Online tutorial: http://fishshell.com/docs/current/tutorial.html
- Documentation: http://fishshell.com/docs/current/index.html
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
-
Latest Cinnamon Desktop Releases with a Bold New Look
Just in time for the holidays, the developer of the Cinnamon desktop has shipped a new release to help spice up your eggnog with new features and a new look.
-
Armbian 24.11 Released with Expanded Hardware Support
If you've been waiting for Armbian to support OrangePi 5 Max and Radxa ROCK 5B+, the wait is over.
-
SUSE Renames Several Products for Better Name Recognition
SUSE has been a very powerful player in the European market, but it knows it must branch out to gain serious traction. Will a name change do the trick?
-
ESET Discovers New Linux Malware
WolfsBane is an all-in-one malware that has hit the Linux operating system and includes a dropper, a launcher, and a backdoor.
-
New Linux Kernel Patch Allows Forcing a CPU Mitigation
Even when CPU mitigations can consume precious CPU cycles, it might not be a bad idea to allow users to enable them, even if your machine isn't vulnerable.
-
Red Hat Enterprise Linux 9.5 Released
Notify your friends, loved ones, and colleagues that the latest version of RHEL is available with plenty of enhancements.
-
Linux Sees Massive Performance Increase from a Single Line of Code
With one line of code, Intel was able to increase the performance of the Linux kernel by 4,000 percent.
-
Fedora KDE Approved as an Official Spin
If you prefer the Plasma desktop environment and the Fedora distribution, you're in luck because there's now an official spin that is listed on the same level as the Fedora Workstation edition.
-
New Steam Client Ups the Ante for Linux
The latest release from Steam has some pretty cool tricks up its sleeve.
-
Gnome OS Transitioning Toward a General-Purpose Distro
If you're looking for the perfectly vanilla take on the Gnome desktop, Gnome OS might be for you.