fish (Unix shell)

{{Short description|User-friendly interactive Unix shell}}

{{Multiple issues|

{{More citations needed|date=May 2019}}

{{Tone|date=July 2024}}

{{Missing information|Fish's history|date=March 2022}}

}}

{{Infobox software

| name = Fish

| logo = Fish icon.png

| screenshot = Fish shell 4.0.0 screenshot.webp

| screenshot size = 300px

| caption = Version 4.0.0 of fish

| author = Axel Liljencrantz

| developer = Fish-shell developers{{cite web| url = https://github.com/fish-shell?tab=members| title = fish shell team members | publisher = GitHub.com| date = | access-date = 2021-07-28}}

| released = {{release date and age|2005|02|13|df=yes}}

| latest release version = {{wikidata|property|reference|edit|P348}}

| latest release date = {{wikidata|qualifier|P348|P577}}

| programming language = Rust{{cite web |title=fish-shell 4.0b1, now in Rust |url=https://fishshell.com/blog/fish-4b/ |website=fishshell.com |access-date=18 December 2024}}

| operating system = Unix-like

| genre = Unix shell

| license = GPL-2.0-only[http://fishshell.com/docs/current/license.html fishshell.com] License for fish

| website = {{URL|http://fishshell.com}}

}}

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,{{cite web|url=https://lwn.net/Articles/136232|publisher=Linux Weekly News|title=Fish - A user-friendly shell|access-date=2010-03-24|date=2005-05-17|author=Liljencrantz, Axel}} and does not adhere to POSIX shell standards by design.{{cite web|url=https://fishshell.com/docs/current/design.html|title=Fish docs: design|access-date=2021-04-09}}

Features

Fish displays incremental suggestions as the user types, based on command history and the current directory. This functions similarly to Bash's {{keypress|Ctrl|R}} 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.{{Cite web |title=Writing your own completions |url=https://fishshell.com/docs/current/completions.html |url-status=live |archive-url=https://web.archive.org/web/20240831075627/https://fishshell.com/docs/current/completions.html |archive-date=2024-08-31 |website=fish shell}}

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.[https://www.linux.com/news/cli-magic-enhancing-shell-fish Linux.com]. CLI Magic: Enhancing the shell with fish. Retrieved 2010-03-24.

Syntax

The syntax resembles a POSIX compatible shell (such as Bash), but deviates in many ways{{cite web|last1=Paul|first1=Ryan|title=An in-depth look at fish: the friendly interactive shell|url=https://arstechnica.com/information-technology/2005/12/linux-20051218/2/|website=Ars Technica|date=19 December 2005 |access-date=10 March 2015|quote=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.}}

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

> set foo bar

> echo $foo

bar

  1. Command substitution
  2. Assign the output of the command 'pwd' into the variable 'wd'.
  3. 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 $B

3 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 $A

3 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 in order 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"

fi

done

Workaround:

found=''

while read dev mnt rest; do

if test "$mnt" = "/"; then

found="$dev"

fi

done < /etc/fstab

Fish example:

set found ''

cat /etc/fstab | while read dev mnt rest

if test "$mnt" = "/"

set found $dev

end

end

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

  • Advanced tab completion (with support for writing custom completions).
  • Syntax highlighting with extensive error checking.
  • Support for the X clipboard.
  • Smart terminal handling based on terminfo.
  • Searchable command history.
  • Web-based configuration ([https://fishshell.com/docs/current/cmds/fish_config.html fish_config]).

Bash/fish translation table

class="wikitable"
style="width: 15%" | Featurestyle="width: 25%" | Bash syntaxstyle="width: 45%" | fish syntaxstyle="width: 15%" |Comment
variable expansion:
with word splitting and glob interpretation
$var

or

${var[@]}

or

${var[*]}

| {{No|deliberately omitted}}

Identified as a primary cause of bugs in posix compatible shell languages
variable expansion:
scalar
"$var"

| {{No|deliberately omitted}}

Every variable is an array
variable expansion:
array
"${var[@]}"$var

|rowspan=2| 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{{keypress|Ctrl|X}},{{keypress|Ctrl|E}}{{keypress|Alt|E}}Upon invocation, moves line input to a text editor
evaluate line input{{keypress|Ctrl|Alt|E}}{{n/a}}{{cite web|url=https://github.com/fish-shell/fish-shell/issues/751|title=RFC: Add binding to expand/evaluate tokens on commandline|website=GitHub |date=2013-05-16|access-date=2021-04-09}}Evaluates expressions in-place on the line editor
history completion{{keypress|Ctrl|R}}{{Yes|implicit}}
history substitution!!{{No|deliberately omitted}}Not discoverable
explicit subshell(expression)

|

fish -c expression

command substitution"$(expression)"

|

"$(expression)" or (expression | string collect)

|

process substitution<(expression)

|

(expression | psub)

|rowspan=5| Command, not syntax

logical operators

!cmd && echo FAIL

echo OK

|

not command

and echo FAIL

or echo OK

variable assignmentvar=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

|class="nowrap"|

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 variableexport var

|

set --export var

|rowspan=5| Options discoverable via tab completion

function-local variablelocal var

| {{yes|by default}}

scope-local variable{{No|no equivalent}}

|

set --local var

remove variableunset var

|

set --erase var

check if a variable existstest -v var

|

set --query var

array initializationvar=( a b c )

|

set var a b c

|rowspan=6| 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[1]

argument vector:
length
$#

|

(count $argv)

argument vector:
shift
shift

|

set --erase argv[1]

array representation in environment variablesPATH="$PATH:$HOME/.local/bin"set PATH $PATH $HOME/.local/binfish 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 runLANG=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.{{cite web|title=printf does not support \e|url=https://github.com/fish-shell/fish-shell/issues/910|website=fish issues|access-date=24 March 2016|date=11 Jul 2013}}

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

{{Portal|Free and open-source software}}

References

{{Reflist|colwidth=35em|refs=

{{cite web|title=Bash Pitfalls|url=http://mywiki.wooledge.org/BashPitfalls|quote=This page shows common errors that Bash programmers make. (...) You will save yourself from many of these pitfalls if you simply always use quotes and never use word splitting for any reason! Word splitting is a broken legacy misfeature inherited from the Bourne shell that's stuck on by default if you don't quote expansions. The vast majority of pitfalls are in some way related to unquoted expansions, and the ensuing word splitting and globbing that result.|access-date=2016-07-10}}

}}