About me: My name is Solène Rapenne, pronouns she/her. I like learning and sharing knowledge. Hobbies: '(BSD OpenBSD Qubes OS Lisp cmdline gaming security QubesOS internet-stuff). I love percent and lambda characters. OpenBSD developer solene@. No AI is involved in this blog.

Contact me: solene at dataswamp dot org or @solene@bsd.network (mastodon).

I'm a freelance OpenBSD, FreeBSD, Linux and Qubes OS consultant, this includes DevOps, DevSecOps, technical writing or documentation work.

If you enjoy this blog, you can sponsor my open source work financially so I can write this blog and contribute to Free Software as my daily job.

Playing with a new shell: fish

Written by Solène, on 05 September 2021.
Tags: #openbsd #shell

Comments on Fediverse/Mastodon

1. Introduction §

Today I'll introduce you to the interactive shell fish. Usually, Linux distributions ships bash (which can be a hidden dash, a limited shell), MacOS is providing zsh and OpenBSD ksh. There are other shells around and fish is one of them.

But fish is not like the others.

fish shell project website

2. What make it special? §

Here is a list of biggest changes:

  • suggested input based on commands available
  • suggested input based on history (even related to the current directory you are in!)
  • not POSIX compatible (the usual shell syntax won't work)
  • command completion works out of the box (no need for extensions like "ohmyzsh")
  • interconnected processes: updating a variable can be done into every opened shells

Asciinema recording showing history features and also fzf integration

3. Making history more powerful with fzf §

fzf is a simple utility for searching data among a file (the history file in that case) in fuzzy mode, meaning in not a strict matching, on OpenBSD I use the following configuration file in ~/.config/fish/config.fish to make fzf active.

When pressing ctrl+r with some history available, you can type any words you can think about an old command like "ssh bar" and it should return "ssh foobar" if it exists.

source /usr/local/share/fish/functions/fzf-key-bindings.fish

fzf is absolutely not related to fish, it can certainly be used in some other shells.

github: fzf project

4. Tips §

4.1. Disable caret character for redirecting to stderr §

The defaults works pretty well but as I said before, fish is not POSIX compatible, meaning some habits must be changed. By default, ^ character like in "grep ^foobar" is the equivalent of 2> which is very misleading.

# make typing ^ actually inserting a "^" and not stderr redirect
set -U fish_features stderr-nocaret qmark-noglob

4.2. Web GUI for customizing your shell §

If you want to change behaviors or colors of your shell, just type "fish_config" while in a shell fish, it will run a local web server and open your web browser.

4.3. Validating a suggestion §

When you type a command and you see more text suggested as you type the command you can press ctrl+e to validate the suggestion. If you don't care about the suggestion, continue typing your command.

4.4. Get the return value of latest command §

In fish, you want to read $status and not $? , that variable doesn't exist in fish.

4.5. Syntax changes §

Because it's not always easy to find what changed and how, here is a simple reminder that should cover most of your needs:

  • loops (no do keyword, ends with end): for i in 1 2 3 ; echo $i ; end
  • condition (no then, ends with end): if something ; echo true ; end
  • inline command (no dollar sign): (date +%s)
  • export a variable: set -x EDITOR kak
  • return value of last command: $status

5. Conclusion §

I love this shell. I've been using the shell that come with my system since forever, and a few months ago I wanted to try something different, it felt weird at first but over time I found it very convenient, especially for git commands or daily tasks, suggesting me exactly the command I wanted to type in that exact directory.

Obviously, as the usual syntax changes, it may not please everyone and it's totally fine.