Fish (Unix shell) explained

fish
Screenshot Size:300px
Author:Axel Liljencrantz
Developer:Fish-shell developers[1]
Programming Language:Rust since v4.0,[2] C++ up to v3.7.1[3]
Operating System:Unix-like
Genre:Unix shell
License:GPL-2.0-only[4]

fish (friendly interactive shell; stylized in lowercase) is a Unix-like shell with a focus on interactivity and usability. fish is designed to be feature-rich by default, rather than highly configurable,[5] and does not adhere to POSIX shell standards by design.[6]

Features

fish displays incremental suggestions as the user types, based on command history and the current directory. This functions similarly to Bash's history search, but is always on, giving the user continuous feedback while typing commands. fish also includes feature-rich tab completion, with support for expanding file paths (with wildcards and brace expansion), environment variables, and command-specific completions. Command-specific completions, including options with descriptions, can be to some extent generated from the commands' man pages, but custom completions can also be included with software or written by users of the shell.[7]

The creator of fish preferred to add new features as commands rather than syntax. This made features more discoverable, as the built-in features allow searching commands with options and help texts. Functions can also include human readable descriptions. A special help command gives access to all the fish documentation in the user's web browser.[8]

Syntax

The syntax resembles a POSIX compatible shell (such as Bash), but deviates in many ways[9]

  1. Variable assignment
    1. Set the variable 'foo' to the value 'bar'.
  2. fish doesn't use the = operator, which is inherently whitespace sensitive.
  3. The 'set' command extends to work with arrays, scoping, etc.

> set foo bar> echo $foobar

  1. Command substitution
    1. Assign the output of the command 'pwd' into the variable 'wd'.
  2. fish doesn't use backticks (``), which can't be nested and may be confused with single quotes (' ').

> set wd (pwd)> set wd $(pwd) # since version 3.4> echo $wd~

  1. Array variables. 'A' becomes an array with 5 values:

> set A 3 5 7 9 12

  1. Array slicing. 'B' becomes the first two elements of 'A':

> set B $A[1 2]> echo $B3 5

  1. You can index with other arrays and even command
  2. substitution output:

> echo $A[(seq 3)]3 5 7

  1. Erase the third and fifth elements of 'A'

> set --erase A[$B]> echo $A3 5 9

  1. for-loop, convert jpegs to pngs

> for i in *.jpg convert $i (basename $i .jpg).png end

  1. fish supports multi-line history and editing.
  2. Semicolons work like newlines:

> for i in *.jpg; convert $i (basename $i .jpg).png; end

  1. while-loop, read lines /etc/passwd and output the fifth
  2. colon-separated field from the file. This should be
  3. the user description.

> while read line set arr (echo $line|tr : \n) echo $arr[5] end < /etc/passwd

  1. String replacement (replacing all i by I)

> string replace -a "i" "I" "Wikipedia"WIkIpedIa

No implicit subshell

Some language constructs, like pipelines, functions and loops, have been implemented using so called subshells in other shell languages. Subshells are child programs that run a few commands to perform a task, then exit back to the parent shell. This implementation detail typically has the side effect that any state changes made in the subshell, such as variable assignments, do not propagate to the main shell. fish never creates subshells for language features; all builtins happen within the parent shell.

  1. This will not work in many other shells, since the 'read' builtin
  2. will run in its own subshell. In Bash, the right side of the pipe
  3. can't have any side effects. In ksh, the below command works, but
  4. the left side can't have any side effects. In fish and zsh, both
  5. sides can have side effects.

> cat *.txt | read line

Variable assignment example

This Bash example doesn't do what it seems: because the loop body is a subshell, the update to $found is not persistent.found=cat /etc/fstab | while read dev mnt rest; do if test "$mnt" = "/"; then found="$dev" fidone

Workaround:found=while read dev mnt rest; do if test "$mnt" = "/"; then found="$dev" fidone < /etc/fstab

Fish example:set found cat /etc/fstab | while read dev mnt rest if test "$mnt" = "/" set found $dev endend

Universal variables

fish has a feature known as universal variables, which allows a user to permanently assign a value to a variable across all the user's running fish shells. The variable value is remembered across logouts and reboots, and updates are immediately propagated to all running shells.

  1. This will make emacs the default text editor. The '--universal' (or '-U') tells fish to
  2. make this a universal variable.

> set --universal EDITOR emacs

  1. This command will make the current working directory part of the fish
  2. prompt turn blue on all running fish instances.

> set --universal fish_color_cwd blue

Other features

Bash/fish translation table

Feature Bash syntax fish syntax Comment
variable expansion:
with word splitting and glob interpretation
$varor $or $Identified as a primary cause of bugs in posix compatible shell languages
variable expansion:
scalar
"$var"Every variable is an array
variable expansion:
array
"$"$varQuoting not necessary to suppress word splitting and glob interpretation. Instead, quoting signifies serialization.
variable expansion:
as a space separated string
"$""$var"
edit line in text editor , Upon invocation, moves line input to a text editor
evaluate line input [11] Evaluates expressions in-place on the line editor
history completion
history substitution !Not discoverable
explicit subshell (expression)fish -c expression
command substitution "$(expression)""$(expression)" or (expression string collect)
process substitution <(expression)(expression psub)Command, not syntax
logical operators cmd && echo FAIL echo OKnot commandand echo FAILor echo OK
variable assignment var=valueset var value
string processing:
replace
"$"string replace alice bob $HOME
string processing:
remove prefix or suffix pattern, non-greedily or greedily
var=a.b.c"$" #b.c"$" #c"$" #a.b"$" #astring replace --regex '.*?\.(.*)' '$1' a.b.c #b.cstring replace --regex '.*\.(.*)' '$1' a.b.c #cstring replace --regex '(.*)\..*' '$1' a.b.c #a.bstring replace --regex '(.*?)\..*' '$1' a.b.c #a
export variable export varset --export varOptions discoverable via tab completion
function-local variable local var
scope-local variable set --local var
remove variable unset varset --erase var
check if a variable exists test -v varset --query var
array initialization var=(a b c)set var a b cEvery variable is an array
array iteration for i in "$"; do echo "$i"done for i in $var echo $iend
argument vector:
all arguments
"$@"$argv
argument vector:
indexing
"$1"$argv[1]
argument vector:
length
$#(count $argv)
argument vector:
shift
shiftset --erase argv[1]
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 python3env LANG=C.UTF-8 python3env 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.
$'\e' \eprintf '\e' works in both shells; their printf builtins are both compatible with the GNU printf standalone program.[12]
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).

See also

External links

Notes and References

  1. Web site: fish shell team members . 10 November 2025 . GitHub.com.
  2. Web site: 17 December 2024 . fish-shell 4.0b1, now in Rust . 18 December 2024 . fishshell.com.
  3. Web site: fish 4.0.0 (released February 27, 2025) . 16 August 2025 . fishshell.com.
  4. Web site: License . 10 November 2025 . fishshell.com.
  5. Web site: Liljencrantz . Axel . 17 May 2005 . Fish – A user-friendly shell . 24 March 2010 . Linux Weekly News.
  6. Web site: Fish docs: design . 9 April 2021 . fishshell.com.
  7. Web site: Writing your own completions . live . https://web.archive.org/web/20240831075627/https://fishshell.com/docs/current/completions.html . 31 August 2024 . 10 November 2025 . fishshell.com.
  8. Web site: 13 November 2006 . CLI Magic: Enhancing the shell with fish . 10 November 2025 . Linux.com . en-US.
  9. Web site: Paul . Ryan . 19 December 2005 . An in-depth look at fish: the friendly interactive shell . 10 March 2015 . . the Posix syntax has several missing or badly implemented features, including variable scoping, arrays, and functions. For this reason, fish strays from the Posix syntax in several important places..
  10. Web site: fish_config – start the web-based configuration interface . 10 November 2025 . fishshell.com.
  11. Web site: dag . 16 May 2013 . RFC: Add binding to expand/evaluate tokens on commandline . 9 April 2021 . GitHub.
  12. Web site: Nordal . Andreas . 11 July 2013 . printf does not support \e . 24 March 2016 . fish issues.