fish (Unix shell)
Original author(s) | Axel Liljencrantz |
---|---|
Developer(s) | ridiculousfish, siteshwar, JanKanis[1] |
Initial release | 13 February 2005 |
Stable release | 2.2.0
/ 12 July 2015[2] |
Repository | |
Operating system | Unix-like |
Type | Unix shell |
License | GPL v2[3] |
Website | fishshell.com |
The friendly interactive shell (fish) is a Unix shell that attempts to be more interactive and user-friendly than former shells. 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.[4] fish is considered an "exotic shell", in that its syntax derives from neither the Bourne shell (ksh, bash, zsh) nor the C shell (csh, tcsh). Also unlike previous shells, which do not have certain features enabled by default as a means of saving system resources, fish includes all features enabled by default.
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 featureful, 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' 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.[5]
Syntax
The syntax resembles a POSIX compatible shell (such as bash), but deviates in important ways where the creators believe the POSIX shell was badly designed.[6]
# Variable assignment, set the variable 'foo' to the
# value 'bar'. Fish doesn't use the = operator, since
# it is inherently whitespace sensitive. Also, the set
# command easily 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 ``
# since they can't be nested and look too much like ' '.
# Don't use $() since $ is only used for variable
# expansion in fish.
> set wd (pwd)
> 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
# Semicolons work like newlines:
> for i in *.jpg; convert $i (basename $i .jpg).png; end
# but the multi-line form is comfortable to use because
# fish supports multi-line history and editing.
# while-loop, read lines /etc/passwd and output the fifth
# colon-separated field from the file. This should be
# the user description.
> cat /etc/passwd | while read line
set arr (echo $line|tr : \n)
echo $arr[5]
end
One important difference between fish and other shells is the lack of subshells. Many tasks like pipelines, functions and loops are implemented using so called subshells in other languages. Subshells are simply child programs that run a few commands for the shell and then exit. Unfortunately, changes made inside a subshell do not have any effect in the main shell, meaning that actions such as variable assignments and the use of many builtin functions do not work as expected. 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
Helpful error messages
Error messages in fish are designed to actually tell the user what went wrong and what can be done about it.[7]
> 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 ${foo}bar
fish: Did you mean {$VARIABLE}? 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 $(pwd)
fish: Did you mean (COMMAND)? 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.
# This will make emacs the default text editor. The '-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
- Advanced tab completion.
- Syntax highlighting with extensive error checking.
- Support for the X clipboard.
- Smart terminal handling based on terminfo.
- Searchable command history.
Version 2 adds:
- Autosuggestions
- 256 terminal colors
- Web-based configuration
- Improved performance (by having more builtins).
bash/fish Translation table
Feature | bash syntax | fish syntax | Comment |
---|---|---|---|
variable expansion |
"$var" |
$var |
Variables do not explode if unquoted |
history completion | Ctrl+R | implicit | |
history substitution |
sudo !! |
deliberately omitted | Not discoverable |
subshell |
(expression) |
deliberately omitted | Deemed useless |
command substitution |
"$(expression)" |
(expression) |
|
process substitution |
<(expression) |
(expression | psub) |
command, not syntax |
variable assignment |
var=value |
set var value
| |
export variable |
export var |
set -x var |
Options discoverable via tab completion |
declare local variable |
local var |
set -l var | |
remove variable |
unset var |
set -e var | |
array initialization |
var=( a b c ) |
set var a b c
|
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 (count) |
$# |
(count $argv) | |
argument vector (shift) |
shift
|
set argv $argv[2..-1]
| |
array representation in environment variables | PATH="$PATH:$HOME/.local/bin"
|
set PATH $PATH $HOME/.local/bin
|
fish assumes colon as array delimiter for translating variables to and from the environment. This aligns with many common variables, like $PATH and $LS_COLORS. |
export and run |
LANG=C.UTF-8 python3 |
begin
set -xl LANG C.UTF-8
python3
end
|
env LANG=C.UTF-8 python3 works in any shell, as env is a separate program.
|
logical operators | [[ ! $var = "" ]] && echo nonempty || echo empty
|
not test "$var" = ""
and echo nonempty
or echo empty
|
In bash, [[ is special syntax, not a command, that allows for not quoting the variable. In fish, the bare unquoted variable is an array of zero or more arguments, but in quotes serializes to exactly one argument, as expected by the test command. |
escape sequence interpretation | $'\e'
|
\e |
Fish interprets unquoted escape sequences. The printf command also interprets escape sequences, which in both fish and bash is a builtin that is compatible with GNU printf; printf '\e' produces the same result in both shells.[8] |
See also
References
- ^ "fish shell team members". GitHub.com. Retrieved 2013-05-21.
- ^ fishshell.com Release Notes for fish 2.2.0
- ^ fishshell.com License for fish
- ^ Linux Weekly News. Fish - A user-friendly shell. Retrieved 2010-03-24.
- ^ Linux.com. CLI Magic: Enhancing the shell with fish. Retrieved 2010-03-24.
- ^ Paul, Ryan. "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.
- ^ Handle With Linux. Afraid of the command line? Try fish. Archived from the original on 2012-07-19.
- ^ "printf does not support \e". fish issues. 11 Jul 2013. Retrieved 24 March 2016.
External links
- Official website – containing documentation and downloads
- fish on GitHub (active)
- fish on Gitorious (obsolete)
- fish on SourceForge (obsolete)
- Fish-users – general discussion list for fish users