Everything you do on the Linux command line involves a shell, whether you pay it any mind or not. It’s probably Bash, possibly Zsh, but either way, it’s highly relevant to all shell scripting, big and small.
The Fish shell is an alternative, with some particularly exciting features for programmers. Even if you’re just coding-curious, you can benefit from checking out this intriguing shell.
What is Fish, and how do you start using it?
A friendly, interactive shell that goes beyond POSIX
Fish is an often-overlooked shell, mainly because it’s usually not the default. CachyOS—a distro which has, admittedly, gained a lot of recent interest, is a rare exception.
Changing your shell is an easy process, but discoverability and familiarity are likely factors in most of us sticking to Bash or Zsh. But a shell is fundamental to running and scripting terminal commands, so it’s worth learning a bit about it; the benefits you feel should be widespread.
Just like every other shell, Fish has its own scripting language built in. A shell scripting language is usually easy to learn and use, partly because it’s just an extension of your typical command-line use.
Fish also comes packed with beginner-friendly features from syntax highlighting and autocomplete to web-based configuration and a super-powered version of the standard alias, called abbreviations.
Unlike shells such as Bash and Zsh, Fish does not follow the POSIX standard. That means that Fish scripts won’t necessarily run correctly in other shells, so if you’re building software for a wide audience, you’ll need to bear this in mind. For all your personal work, however, Fish is a perfect fit.
To start using Fish, begin by installing it with Homebrew (Mac) and brew install fish, pacman (Arch) and pacman -S fish, dnf (Fedora) and dnf install fish, or whichever method is appropriate for your system.
Once installed, you can try it out just by running the command fish. To make the change permanent, either use the chsh command or your system’s GUI equivalent.
Fish is beginner-friendly
Everyone benefits from Fish’s usability, even experienced users
The first thing you’ll notice upon starting Fish is its friendly welcome message and unique prompt:
However, as soon as you start interacting with the shell, you’ll notice its excellent support for autosuggestions and autocomplete. For example, type ls followed by a Tab and you’ll see a list of all commands you can run beginning with ls:
Autosuggestions also work, out of the box, for options and other arguments, like filenames:
This feature is not only a convenient way of reducing the time it takes to type a command in full, but it also aids discovery. With a simple list of options, for example, it’s much easier to scan and find a useful feature than it is when scrolling through pages of the manual.
Syntax highlighting is another great feature, although it can seem a bit unnecessary until you actually try it. Going far beyond just making things look nice, syntax highlighting can help you understand context and semantics at a glance. Take error reporting, for example; if you typo a command in Fish, it’s immediately obvious:
Fish also comes with built-in web pages for help and configuration. To access either, simply run the help or fish_config command, respectively. Your web browser should open automatically, displaying the relevant page:
Fish’s web interfaces all run locally, so you can consult help or configure the system even if you’re offline.
Fish is approachable and easy for any user, from beginners to experts. As a programmer, you’ll find that its convenient shortcuts will benefit your day-to-day work as much as any other user. But its features can also help with the specific task of programming, too.
Fish is also programmer-friendly
The shell includes features of particular use to coders
A scripting language is core to any shell, and Fish differentiates itself from shells like Bash and Zsh with a language that looks quite different. In general, Fish favours commands over symbolic syntax, so instead of $((i+1)), you’d write math $i + 1.
Fish will help with certain aspects of a migration from Bash by reacting to common mistakes with useful messages:
Fish’s default prompt also shows the exit code for the last command, which is a great way of discovering which codes are used, as well as spotting when something’s gone wrong:
Argument parsing is a very common task for more complex shell scripts, which Bash doesn’t make all too easy. With Fish, you can use the argparse command to define the options that your script supports and automatically populate corresponding variables.
The fish_config command we saw earlier contains some very useful information, like a list of current variable values and key bindings. Of particular relevance is its “functions” page, which allows you to browse defined functions:
You can use this to not only view your own functions, but also browse the many functions that come built into Fish. These include some really useful code, but they’re also great for learning the language.
Another great feature worth checking out is the function command—used to define a function—and the options that it supports. You can use –wraps to declare that your function wraps another command, in which case it will automatically inherit that command’s tab completions. Meanwhile, you can use –description to add brief documentation, which is also used in tab completions.
Why use Fish instead of Bash?
As a programmer, it pays to be ever-curious, so based on that alone, it’s worth checking out the default shell from the currently in-vogue Linux distro. But Fish offers many useful features that will make your programming life more enjoyable and enable you to write programs with greater ease.
