Fish (Unix shell)
Adapted from Wikipedia · Discoverer experience
Fish, short for the friendly interactive shell, is a special kind of program used on computers that work with Unix-like systems. It helps people type commands more easily and makes working on the computer more fun. Unlike some other shells, fish comes with many helpful features already turned on, so users don’t have to set everything up themselves.
One big difference about fish is that it doesn’t follow the POSIX shell standards. This means it works a little differently from some other shells, but these differences are meant to make it easier and more pleasant to use. Because of its focus on being user-friendly, fish is popular with people who want a shell that feels comfortable and intuitive right from the start.
Fish is especially good for people who spend a lot of time typing commands on the computer. It offers suggestions and helps prevent mistakes, making tasks faster and easier. If you use a Unix-like system and want a shell that is both powerful and easy to use, fish is a great choice.
Features
Fish helps you while you type by showing suggestions based on what you've typed before and what's in your folder. It's like having a helper that works all the time. It also makes it easy to finish commands with the press of a button, recognizing files, folders, and special settings.
The person who made fish chose to add new tools as commands you can type, instead of changing the way you write. This makes it easier to find and learn about these tools. There is even a special command that opens all the help information in your web browser for easy reading.
Syntax
The way you type commands in fish looks similar to other shells like Bash, but it works differently in many ways.
You can set variables using the set command instead of the = sign. For example, to set a variable called foo to bar, you type set foo bar. You can also use variables that hold lists of items, called arrays. fish also lets you run loops to process files, like turning all JPEG images into PNG images.
fish does not use special separate areas called subshells for some tasks. This means changes you make inside a loop or command will still work after the command finishes. For example, you can read a file line by line and change variables based on what you read, and those changes will stay.
Other features
Fish has many helpful tools to make using the computer easier. It offers smart suggestions when you type, helping you finish commands quickly. It also shows colors to make your typing easier to read and can find mistakes for you.
Fish can work with things outside the computer, like copying and pasting text. It also remembers what commands you have used before, so you can search through them easily. There is even a way to change settings using a web browser.
Bash/fish translation table
| Feature | Bash syntax | fish syntax | Comment |
|---|---|---|---|
| variable expansion: with word splitting and glob interpretation | $var or ${var[@]}or ${var[*]} | deliberately omitted | Identified as a primary cause of bugs in posix compatible shell languages |
| variable expansion: scalar | "$var" | deliberately omitted | Every variable is an array |
| variable expansion: array | "${var[@]}" | $var | Quoting not necessary to suppress word splitting and glob interpretation. Instead, quoting signifies serialization. |
| variable expansion: as a space separated string | "${var[*]}" | "$var" | |
| edit line in text editor | Ctrl+X,Ctrl+E | Alt+E | Upon invocation, moves line input to a text editor |
| evaluate line input | Ctrl+Alt+E | —N/a | Evaluates expressions in-place on the line editor |
| history completion | Ctrl+R | implicit | |
| history substitution | !! | deliberately omitted | Not discoverable |
| explicit subshell | (expression) | fish -c expression | |
| command substitution | "$(expression)" | "$(expression)" or (expression | string collect) | |
| process substitution | (expression | psub) | Command, not syntax | |
| logical operators | !cmd && echo FAIL || echo OK
| not command
and echo FAIL
or echo OK
| |
| variable assignment | var=value | set var value
| |
| string processing: replace | "${HOME/alice/bob}" | string replace alice bob $HOME | |
| string processing: remove prefix or suffix pattern, non-greedily or greedily | var=a.b.c
"${var#*.}" #b.c
"${var##*.}" #c
"${var%.*}" #a.b
"${var%%.*}" #a
| string replace --regex '.*?\.(.*)' '$1' a.b.c #b.c
string replace --regex '.*\.(.*)' '$1' a.b.c #c
string replace --regex '(.*)\..*' '$1' a.b.c #a.b
string replace --regex '(.*?)\..*' '$1' a.b.c #a
| |
| export variable | export var | set --export var | Options discoverable via tab completion |
| function-local variable | local var | by default | |
| scope-local variable | no equivalent | set --local var | |
| remove variable | unset var | set --erase var | |
| check if a variable exists | test -v var | set --query var | |
| array initialization | var=( a b c ) | set var a b c
| Every variable is an array |
| array iteration | for i in "${var[@]}"; do
echo "$i"
done
| for i in $var
echo $i
end
| |
| argument vector: all arguments | "$@" | $argv | |
| argument vector: indexing | "$1" | $argv | |
| argument vector: length | $# | (count $argv) | |
| argument vector: shift | shift
| set --erase argv
| |
| array representation in environment variables | PATH="$PATH:$HOME/.local/bin"
| set PATH $PATH $HOME/.local/bin
| fish assumes colon as array delimiter for translating variables to and from the environment. This aligns with many array-like environment variables, like $PATH and $LS_COLORS. |
| export and run | LANG=C.UTF-8 python3 | env LANG=C.UTF-8 python3
| env LANG=C.UTF-8 python3 works in any shell, as env is a standalone program. |
| arithmetic | $((10/3)) | math '10/3' | expr 10 / 3 works in any shell, as expr is a standalone program. |
| escape sequence | $'\e'
| \e | printf '\e' works in both shells; their printf builtins are both compatible with the GNU printf standalone program. |
| single quoted string: escape sequences | 'mom'\''s final backslash: \'
| 'mom\'s final backslash: \\'
| Bash only requires replacement of the single quote itself in single quoted strings, but the replacement is 4 characters long. The same replacement works in fish, but fish supports a regular escape sequence for this, thus requires escaping backslashes too (except permits single backslashes that don't precede another backslash or single quote). |
Related articles
This article is a child-friendly adaptation of the Wikipedia article on Fish (Unix shell), available under CC BY-SA 4.0.
Images from Wikimedia Commons. Tap any image to view credits and license.
Safekipedia