Friendly interactive shell


The friendly interactive shell is a Unix shell that attempts to be more interactive and user-friendly than those with a longer history or those formulated as function-compatible replacements for the aforementioned. The design goal of fish is to give the user a rich set of powerful features in a way that is easy to discover, remember, and use. fish is considered an "exotic shell", in that its syntax derives from neither the Bourne shell nor the C shell. Also unlike previous shells, which disable certain features by default to save system resources, fish enables all features by default.

Highlights

fish has "search as you type" automatic suggestions based on history and current directory.
This is essentially like Bash's 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 for a tab completion instead. Tab-completion is feature-rich, expanding file paths, variables, and many command specific completions. Command-specific completions, including options with descriptions, can to some extent be generated from the commands' manpages.
Fish has few syntactic rules, preferring 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.

Syntax

The syntax resembles a POSIX compatible shell, but deviates in important ways where the creators believe the POSIX shell was badly designed.

  1. Variable assignment, set the variable 'foo' to the
  2. value 'bar'. Fish doesn't use the = operator, since
  3. it is inherently whitespace sensitive. Also, the set
  4. command easily extends to work with arrays, scoping, etc.
> set foo bar
> echo $foo
bar
  1. Command substitution, assign the output of the command
  2. 'pwd' into the variable 'wd'. Fish doesn't use ``
  3. since they can't be nested and look too much like ' '.
  4. Don't use $ since $ is only used for variable
  5. expansion in fish.
> set wd
> 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
> echo $B
3 5
  1. You can index with other arrays and even command
  2. substitution output:
> echo $A
3 5 7
  1. Erase the third and fifth elements of 'A'
> set --erase A
> echo $A
3 5 9
  1. for-loop, convert jpegs to pngs
> for i in *.jpg
convert $i.png
end
  1. Semicolons work like newlines:
> for i in *.jpg; convert $i.png; end
  1. but the multi-line form is comfortable to use because
  2. fish supports multi-line history and editing.
  3. while-loop, read lines /etc/passwd and output the fifth
  4. colon-separated field from the file. This should be
  5. the user description.
> while read line
set arr
echo $arr
end < /etc/passwd

No implicit subshell

Some language constructs, like pipelines, functions and loops, have been implemented using so called subshells in other shell languages. Subshells are simply child programs that run a few commands for the shell and then exit. Unfortunately, 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, which may surprise the user. Fish never forks off so-called subshells; all builtins are always fully functional.

  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 does not do what it looks like: 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 does not need a workaround:

set found ''
cat /etc/fstab | while read dev mnt rest
if test "$mnt" = "/"
set found $dev
end
end

Helpful error messages

Error messages in fish are designed to actually tell the user what went wrong and what can be done about it.

> foo=bar
fish: Unknown command “foo=bar”. Did you mean “set VARIABLE VALUE”?
For information on setting variable values, see the help section on
the set command by typing “help set”.
> echo $bar
fish: Did you mean ? The '$' character begins a variable
name. A bracket, which directly followed a '$', is not allowed as a
part of a variable name, and variable names may not be zero characters
long. To learn more about variable expansion in fish, type “help
expand-variable”.
> echo $
fish: Did you mean ? In fish, the '$' character is only used
for accessing variables. To learn more about command substitution in
fish, type “help expand-command-substitution”.

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.

  1. This will make emacs the default text editor. The '--universal' 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

Version 2 adds: