This article has multiple issues. Please help improve it or discuss these issues on the talk page . (Learn how and when to remove these messages)
|
Original author(s) | Axel Liljencrantz |
---|---|
Developer(s) | Fish-shell developers [1] |
Initial release | 13 February 2005 |
Stable release | |
Repository | |
Written in | Rust [3] |
Operating system | Unix-like |
Type | Unix shell |
License | GPL-2.0-only [4] |
Website | fishshell |
Fish (or 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. [5] Fish is considered an exotic shell since it does not adhere to POSIX shell standards, at the discretion of its maintainers. [6]
Fish displays incremental suggestions as the user types, based on command history and the current directory. This functions similarly to Bash's Ctrl+R 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. [7]
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. [8]
The syntax resembles a POSIX compatible shell (such as Bash), but deviates in many ways [9]
# 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 3579 12 # Array slicing. 'B' becomes the first two elements of 'A':>set B $A[1 2]>echo$B3 5 # You can index with other arrays and even command # substitution output:>echo$A[(seq 3)]35 7 # Erase the third and fifth elements of 'A'>set --erase A[$B]>echo$A35 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.>whileread 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
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 in order 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.
# 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
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|whilereaddevmntrest;doiftest"$mnt"="/";thenfound="$dev"fidone
Workaround:
found=''whilereaddevmntrest;doiftest"$mnt"="/";thenfound="$dev"fidone</etc/fstab
Fish example:
set found '' cat /etc/fstab |whileread dev mnt rest iftest"$mnt"="/"set found $devendend
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.
# 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
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 [10] |
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 | Ctrl+X,Ctrl+E | Alt+E | Upon invocation, moves line input to a text editor |
evaluate line input | Ctrl+Alt+E | — [11] | Evaluates expressions in-place on the line editor |
history completion | Ctrl+R | implicit | |
history substitution | !! | deliberately omitted | Not discoverable |
explicit subshell | (expression) | fish -c expression | |
command substitution | "$(expression)" |
| |
process substitution | <(expression) | (expression | psub) | Command, not syntax |
logical operators | !cmd&&echoFAIL||echoOK | notcommandandecho FAIL orecho OK | |
variable assignment | var=value | set var value | |
string processing: replace | "${HOME/alice/bob}" | string replace alice bob $HOME | |
string processing: remove prefix or suffix pattern, non-greedily or greedily | var=a.b.c "${var#*.}"#b.c"${var##*.}"#c"${var%.*}"#a.b"${var%%.*}"#a | 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 | |
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 ) | set var a b c | Every variable is an array |
array iteration | foriin"${var[@]}";doecho"$i"done | for i in$varecho$iend | |
argument vector: all arguments | "$@" | $argv | |
argument vector: indexing | "$1" | $argv[1] | |
argument vector: length | $# | (count $argv) | |
argument vector: shift | shift | set --erase argv[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 array-like environment variables, like $PATH and $LS_COLORS. |
export and run | LANG=C.UTF-8 python3 | env LANG=C.UTF-8 python3 | envLANG=C.UTF-8python3 works in any shell, as env is a standalone program. |
arithmetic | $((10/3)) | math '10/3' | expr10/3 works in any shell, as expr is a standalone program. |
escape sequence | $'\e' | \e | printf'\e' works in both shells; their printf builtins are both compatible with the GNU printf standalone program. [12] |
single quoted string: escape sequences | 'mom'\''s final backslash: \' | 'mom\'s final backslash: \\' | 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). |
Bash, short for Bourne-Again SHell, is a shell program and command language supported by the Free Software Foundation and first developed for the GNU Project by Brian Fox. Designed as a 100% free software alternative for the Bourne shell, it was initially released in 1989. Its moniker is a play on words, referencing both its predecessor, the Bourne shell, and the concept of rebirth.
A shell script is a computer program designed to be run by a Unix shell, a command-line interpreter. The various dialects of shell scripts are considered to be command languages. Typical operations performed by shell scripts include file manipulation, program execution, and printing text. A script which sets up the environment, runs the program, and does any necessary cleanup or logging, is called a wrapper.
A Unix shell is a command-line interpreter or shell that provides a command line user interface for Unix-like operating systems. The shell is both an interactive command language and a scripting language, and is used by the operating system to control the execution of the system using shell scripts.
The Bourne shell (sh
) is a shell command-line interpreter for computer operating systems. It first appeared on Version 7 Unix, as its default shell. Unix-like systems continue to have /bin/sh
—which will be the Bourne shell, or a symbolic link or hard link to a compatible shell—even when other shells are used by most users.
The C shell is a Unix shell created by Bill Joy while he was a graduate student at University of California, Berkeley in the late 1970s. It has been widely distributed, beginning with the 2BSD release of the Berkeley Software Distribution (BSD) which Joy first distributed in 1978. Other early contributors to the ideas or the code were Michael Ubell, Eric Allman, Mike O'Brien and Jim Kulp.
In computing, the working directory of a process is a directory of a hierarchical file system, if any, dynamically associated with the process. It is sometimes called the current working directory (CWD), e.g. the BSD getcwd function, or just current directory. When a process refers to a file using a simple file name or relative path (as opposed to a file designated by a full path from a root directory), the reference is interpreted relative to the working directory of the process. So for example a process with working directory /rabbit-shoes that asks to create the file foo.txt will end up creating the file /rabbit-shoes/foo.txt.
In Unix-like and some other operating systems, the pwd
command writes the full pathname of the current working directory to the standard output.
bc, for basic calculator, is "an arbitrary-precision calculator language" with syntax similar to the C programming language. bc is typically used as either a mathematical scripting language or as an interactive mathematical shell.
In some operating systems, including Unix-like systems, a pseudoterminal, pseudotty, or PTY is a pair of pseudo-device endpoints (files) which establish asynchronous, bidirectional communication (IPC) channel between two or more processes.
fstab is a system file commonly found in the directory /etc
on Unix and Unix-like computer systems. In Linux, it is part of the util-linux package. The fstab file typically lists all available disk partitions and other types of file systems and data sources that may not necessarily be disk-based, and indicates how they are to be initialized or otherwise integrated into the larger file system structure.
Command-line completion is a common feature of command-line interpreters, in which the program automatically fills in partially typed commands.
In Unix-like computer operating systems, a pipeline is a mechanism for inter-process communication using message passing. A pipeline is a set of processes chained together by their standard streams, so that the output text of each process (stdout) is passed directly as input (stdin) to the next one. The second process is started as the first process is still executing, and they are executed concurrently.
In computing, a here document is a file literal or input stream literal: it is a section of a source code file that is treated as if it were a separate file. The term is also used for a form of multiline string literals that use similar syntax, preserving line breaks and other whitespace in the text.
A command shell is a command-line interface to interact with and manipulate a computer's operating system.
In computing, tee
is a command in command-line interpreters (shells) using standard streams which reads standard input and writes it to both standard output and one or more files, effectively duplicating its input. It is primarily used in conjunction with pipes and filters. The command is named after the T-splitter used in plumbing.
In computing, mount
is a command in various operating systems. Before a user can access a file on a Unix-like machine, the file system on the device which contains the file needs to be mounted with the mount command. Frequently mount
is used for SD card, USB storage, DVD and other removable storage devices. The command is also available in the EFI shell.
Qshell is an optional command-line interpreter (shell) for the IBM i operating system. Qshell is based on POSIX and X/Open standards. It is a Bourne-like shell that also includes features of KornShell. The utilities are external programs that provide additional functions. The development team of Qshell had to deal with platform-specific issues such as translating between ASCII and EBCDIC. The shell supports interactive mode as well as batch processing and can run shell scripts from Unix-like operating systems with few or no modifications.
A batch file is a script file in DOS, OS/2 and Microsoft Windows. It consists of a series of commands to be executed by the command-line interpreter, stored in a plain text file. A batch file may contain any command the interpreter accepts interactively and use constructs that enable conditional branching and looping within the batch file, such as IF
, FOR
, and GOTO
labels. The term "batch" is from batch processing, meaning "non-interactive execution", though a batch file might not process a batch of multiple data.
In computing, pushd
and popd
are a pair of commands which allow users to quickly switch between the current and previous directory when using the command line. When called, they use a directory stack to sequentially save and retrieve directories visited by the user.
In a Unix shell, the full stop called the dot command (.) is a command that evaluates commands in a computer file in the current execution context. In the C shell, a similar functionality is provided as the source
command, and this name is seen in "extended" POSIX shells as well.
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.
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.