Jump to content

Fish (Unix shell)

From Wikipedia, the free encyclopedia

Template:Short description Template:Multiple issues Template:Lowercase title

Template:Use dmy dates {{#invoke:Infobox|infobox}}Template:Template other {{#invoke:Check for unknown parameters|check|unknown=Template:Main other|preview=Page using Template:Infobox software with unknown parameter "_VALUE_"|ignoreblank=y| AsOf | author | background | bodystyle | caption | collapsetext | collapsible | developer | discontinued | engine | engines | genre | included with | language | language count | language footnote | latest preview date | latest preview version | latest release date | latest release version | latest_preview_date | latest_preview_version | latest_release_date | latest_release_version | licence | license | logo | logo alt | logo caption | logo class | logo size | logo title | logo upright | logo_alt | logo_caption | logo_class | logo_size | logo_title | logo_upright | middleware | module | name | operating system | operating_system | other_names | platform | programming language | programming_language | qid | released | replaced_by | replaces | repo | screenshot | screenshot alt | screenshot class | screenshot size | screenshot title | screenshot upright | screenshot_alt | screenshot_class | screenshot_size | screenshot_upright | service_name | size | standard | title | ver layout | website }}Template:Main other{{#invoke:Check for conflicting parameters|check | template = Template:Infobox software | cat = Template:Main other | logo size; logo_size | logo class; logo_class | logo alt; logo_alt | logo title; logo_title | logo caption; logo_caption | screenshot size; screenshot_size | screenshot upright; screenshot_upright | screenshot class; screenshot_class | screenshot alt; screenshot_alt | screenshot title; screenshot title | latest release version; latest_release_version | latest release date; latest_release_date | latest preview version; latest_preview_version | latest preview date; latest_preview_date | latest release version; latest_release_version | latest release date; latest_release_date | latest preview version; latest_preview_version | latest preview date; latest_preview_date | programming language; programming_language | operating system; operating_system }} 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,[1] and does not adhere to POSIX shell standards by design.[2]

Features

fish displays incremental suggestions as the user types, based on command history and the current directory. This functions similarly to Bash's Template:Keypress 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.[3]

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.[4]

Syntax

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

<syntaxhighlight lang="fish">

  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 </syntaxhighlight>

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. <syntaxhighlight lang="fish">

  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 </syntaxhighlight>

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. <syntaxhighlight lang="bash"> found= cat /etc/fstab | while read dev mnt rest; do

 if test "$mnt" = "/"; then
   found="$dev"
 fi

done </syntaxhighlight>

Workaround: <syntaxhighlight lang="bash"> found= while read dev mnt rest; do

 if test "$mnt" = "/"; then
   found="$dev"
 fi

done < /etc/fstab </syntaxhighlight>

Fish example: <syntaxhighlight lang="fish"> set found cat /etc/fstab | while read dev mnt rest

 if test "$mnt" = "/"
   set found $dev
 end

end </syntaxhighlight>

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. <syntaxhighlight lang="fish">

  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 </syntaxhighlight>

Other features

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[7]
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 Template:Keypress,Template:Keypress Template:Keypress Upon invocation, moves line input to a text editor
evaluate line input Template:Keypress Template:N/a[8] Evaluates expressions in-place on the line editor
history completion Template:Keypress implicit
history substitution !! deliberately omitted 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 <syntaxhighlight lang="bash"> cmd && echo FAIL echo OK

</syntaxhighlight>

<syntaxhighlight lang="fish">

not command and echo FAIL or echo OK</syntaxhighlight>

variable assignment
var=value

<syntaxhighlight lang="fish"> set var value </syntaxhighlight>

string processing:
replace
"${HOME/alice/bob}"
string replace alice bob $HOME
string processing:
remove prefix or suffix pattern, non-greedily or greedily

<syntaxhighlight lang="bash"> var=a.b.c "${var#*.}" #b.c "${var##*.}" #c "${var%.*}" #a.b "${var%%.*}" #a </syntaxhighlight>

<syntaxhighlight lang="fish"> 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 </syntaxhighlight>

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 )

<syntaxhighlight lang="fish"> set var a b c </syntaxhighlight>

Every variable is an array
array iteration <syntaxhighlight lang="bash">

for i in "${var[@]}"; do

 echo "$i"

done </syntaxhighlight> || <syntaxhighlight lang="fish"> for i in $var

 echo $i

end </syntaxhighlight>

argument vector:
all arguments
"$@"
$argv
argument vector:
indexing
"$1"
$argv[1]
argument vector:
length
$#
(count $argv)
argument vector:
shift

<syntaxhighlight lang="bash">shift</syntaxhighlight>

<syntaxhighlight lang="fish"> set --erase argv[1] </syntaxhighlight>

array representation in environment variables <syntaxhighlight lang="bash">PATH="$PATH:$HOME/.local/bin"</syntaxhighlight> <syntaxhighlight lang="fish">set PATH $PATH $HOME/.local/bin</syntaxhighlight> 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
<syntaxhighlight lang="fish">env LANG=C.UTF-8 python3</syntaxhighlight> <syntaxhighlight lang="bash" inline>env LANG=C.UTF-8 python3</syntaxhighlight> works in any shell, as env is a standalone program.
arithmetic
$((10/3))
math '10/3'
<syntaxhighlight lang="bash" inline>expr 10 / 3</syntaxhighlight> works in any shell, as expr is a standalone program.
escape sequence <syntaxhighlight lang="bash">$'\e'</syntaxhighlight>
\e
<syntaxhighlight lang="bash" inline>printf '\e'</syntaxhighlight> works in both shells; their printf builtins are both compatible with the GNU printf standalone program.[9]
single quoted string:
escape sequences
<syntaxhighlight lang="bash">

'mom'\s final backslash: \' </syntaxhighlight> || <syntaxhighlight lang="python"> 'mom\'s final backslash: \\' </syntaxhighlight> || 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

{{#invoke:Portal|portal}}

References

Template:Reflist

|CitationClass=web }} (obsolete)

Template:Unix Shells

  1. ↑ {{#invoke:citation/CS1|citation |CitationClass=web }}
  2. ↑ {{#invoke:citation/CS1|citation |CitationClass=web }}
  3. ↑ {{#invoke:citation/CS1|citation |CitationClass=web }}
  4. ↑ {{#invoke:citation/CS1|citation |CitationClass=web }}
  5. ↑ {{#invoke:citation/CS1|citation |CitationClass=web }}
  6. ↑ {{#invoke:citation/CS1|citation |CitationClass=web }}
  7. ↑ Cite error: Invalid <ref> tag; no text was provided for refs named bash_pitfalls
  8. ↑ {{#invoke:citation/CS1|citation |CitationClass=web }}
  9. ↑ {{#invoke:citation/CS1|citation |CitationClass=web }}