Fish | |
Screenshot Size: | 300px |
Author: | Axel Liljencrantz |
Developer: | Fish-shell developers[1] |
Programming Language: | Rust |
Operating System: | Unix-like |
Genre: | Unix shell |
License: | GPL-2.0-only[2] |
Fish (stylized in lowercase) is a Unix shell with a focus on interactivity and usability. Fish is designed to be feature-rich, rather than highly configurable.[3] Fish is considered an exotic shell since it does not adhere to POSIX shell standards, at the discretion of its maintainers.[4]
Fish has search as you type automatic suggestions based on history and current directory. This functions like Bash's history search, but is always on, giving the user continuous feedback while writing the command line. Users can select suggestions with the arrow keys, or as in Bash, press 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, which can to some extent be generated from the commands' man pages.
The creator of Fish preferred to make features as commands rather than syntax. This made 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.[5]
The syntax resembles a POSIX compatible shell (such as Bash), but deviates in many ways[6]
> set foo bar> echo $foobar
> set wd (pwd)> set wd $(pwd) # since version 3.4> echo $wd~
> set A 3 5 7 9 12
> set B $A[1 2]> echo $B3 5
> echo $A[(seq 3)]3 5 7
> set --erase A[$B]> echo $A3 5 9
> for i in *.jpg convert $i (basename $i .jpg).png end
> for i in *.jpg; convert $i (basename $i .jpg).png; end
> while read line set arr (echo $line|tr : \n) echo $arr[5] end < /etc/passwd
> string replace -a "i" "I" "Wikipedia"WIkIpedIa
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.
> cat *.txt | read line
This Bash example doesn't do what it seems: because the loop body is a subshell, the update to $found
is not persistent.
Workaround:
Fish example:
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.
> set --universal EDITOR emacs
> set --universal fish_color_cwd blue
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 | "$" | $var | Quoting 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 | [7] | 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 OK | ||
variable assignment | var=value | |||
string processing: replace | "$" | string replace alice bob $HOME | ||
string processing: remove prefix or suffix pattern, non-greedily or greedily | ||||
export variable | export var | set --export var | Options discoverable via tab completion | |
function-local variable | local var | |||
scope-local variable | 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) | Every variable is an array | ||
array iteration | ||||
argument vector: all arguments | "$@" | $argv | ||
argument vector: indexing | "$1" | $argv[1] | ||
argument vector: length | $# | (count $argv) | ||
argument vector: shift | ||||
array representation in environment variables | 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 | |||
arithmetic | $((10/3)) | math '10/3' | ||
\e | printf builtins are both compatible with the GNU printf standalone program.[8] | |||
single quoted string: escape sequences | 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). |