Fish_(Unix_shell)

fish (Unix shell)

fish (Unix shell)

User-friendly interactive Unix shell


fish (stylized in lowercase) is a Unix shell with a focus on interactivity and usability. Fish is designed to give the user features by default, rather than by configuration.[4] Fish is considered an exotic shell since it does not adhere to POSIX shell standards, at the discretion of the maintainers.[5]

Quick Facts Original author(s), Developer(s) ...

Highlights

Fish has search as you type automatic suggestions based on history and current directory. This is essentially like Bash's Ctrl+R history search, but because it is always on instead of being a separate mode, the user gets continuous feedback while writing the command line, and can select suggestions with the arrow keys, or as in Bash, press Tab ↹ for a tab completion instead. Tab-completion is feature-rich and has expanding file paths (with wildcards and brace expansion), variables, and many command specific completions. Command-specific completions, including options with descriptions, can to some extent be generated from the commands' man pages.

Fish prefers features as commands rather than syntax. This makes features discoverable in terms of commands with options and help texts. Functions can also carry a human readable description. A special help command gives access to all the fish documentation in the user's web browser.[6]

Syntax

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

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

> set foo bar
> echo $foo
bar
 
# Command substitution
#
# Assign the output of the command 'pwd' into the variable 'wd'. 
# 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
~

# Array variables. 'A' becomes an array with 5 values:
> set A 3 5 7 9 12
# Array slicing. 'B' becomes the first two elements of 'A':
> set B $A[1 2]
> echo $B
3 5
# You can index with other arrays and even command 
# substitution output:
> echo $A[(seq 3)]
3 5 7
# Erase the third and fifth elements of 'A'
> set --erase A[$B]
> echo $A
3 5 9

# for-loop, convert jpegs to pngs
> for i in *.jpg
      convert $i (basename $i .jpg).png
  end

# fish supports multi-line history and editing.
# Semicolons work like newlines:
> for i in *.jpg; convert $i (basename $i .jpg).png; end



# while-loop, read lines /etc/passwd and output the fifth 
# colon-separated field from the file. This should be
# the user description.
> while read line
      set arr (echo $line|tr : \n)
      echo $arr[5]
  end < /etc/passwd

# 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 for the shell and then exit. 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 forks off so-called subshells; all builtins are always fully functional.

# This will not work in many other shells, since the 'read' builtin
# will run in its own subshell. In Bash, the right side of the pipe
# can't have any side effects. In ksh, the below command works, but
# the left side can't have any side effects. In fish and zsh, both
# 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 allow 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.

# This will make emacs the default text editor. The '--universal' (or '-U') tells fish to
# make this a universal variable.
> set --universal EDITOR emacs

# This command will make the current working directory part of the fish
# prompt turn blue on all running fish instances.
> set --universal fish_color_cwd blue

Other features

Bash/fish translation table

More information Feature, Bash syntax ...

See also


References

  1. "fish shell team members". GitHub.com. Retrieved 2021-07-28.
  2. "Release 3.7.1". 19 March 2024. Retrieved 22 March 2024.
  3. fishshell.com License for fish
  4. Liljencrantz, Axel (2005-05-17). "Fish - A user-friendly shell". Linux Weekly News. Retrieved 2010-03-24.
  5. "Fish docs: design". Retrieved 2021-04-09.
  6. Linux.com. CLI Magic: Enhancing the shell with fish. Retrieved 2010-03-24.
  7. Paul, Ryan (19 December 2005). "An in-depth look at fish: the friendly interactive shell". Ars Technica. Retrieved 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.
  8. "Bash Pitfalls". Retrieved 2016-07-10. 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.
  9. "printf does not support \e". fish issues. 11 Jul 2013. Retrieved 24 March 2016.

Share this article:

This article uses material from the Wikipedia article Fish_(Unix_shell), and is written by contributors. Text is available under a CC BY-SA 4.0 International License; additional terms may apply. Images, videos and audio are available under their respective licenses.