Fish (Unix shell)

Last updated
Fish
Original author(s) Axel Liljencrantz
Developer(s) Fish-shell developers [1]
Initial release13 February 2005;18 years ago (2005-02-13)
Stable release
3.6.1 [2]   OOjs UI icon edit-ltr-progressive.svg / 25 March 2023;5 months ago (25 March 2023)
Repository
Written in C++, Rust
Operating system Unix-like
Type Unix shell
License GPL-2.0-only [3]
Website fishshell.com

fish is a Unix shell with a focus on interactivity and usability. Fish is designed to give the user features by default, rather than by configuration. [4] Fish is considered an exotic shell since it does not adhere to POSIX shell standards, at the discretion of the maintainers. [5]

Contents

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 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, can to some extent be generated from the commands' man pages.

Fish prefers 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. [6]

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

# 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 

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 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.

# 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 

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.

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

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 '--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 

Other features

Bash/fish translation table

FeatureBash syntaxfish syntaxComment
variable expansion:
with word splitting and glob interpretation
$var

or

${var[@]}

or

${var[*]}
deliberately omittedIdentified as a primary cause of bugs in posix compatible shell languages [8]
variable expansion:
scalar
"$var"
deliberately omittedEvery 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 editorCtrl+X,Ctrl+EAlt+EUpon invocation, moves line input to a text editor
evaluate line inputCtrl+Alt+E [9] Evaluates expressions in-place on the line editor
history completionCtrl+Rimplicit
history substitution !!deliberately omittedNot 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&&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 variableno 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. [10]
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).

See also

Related Research Articles

<span class="mw-page-title-main">Bash (Unix shell)</span> GNU replacement for the Bourne shell

Bash is a Unix shell and command language written by Brian Fox for the GNU Project as a free software replacement for the Bourne shell. First released in 1989, it has been used as the default login shell for most Linux distributions. Bash was one of the first programs Linus Torvalds ported to Linux, alongside GCC. A version is also available for Windows 10 and Windows 11 via the Windows Subsystem for Linux. It is also the default user shell in Solaris 11. Bash was also the default shell in versions of Apple macOS from 10.3 to 10.15, which changed the default shell to zsh, although Bash remains available as an alternative shell.

<span class="mw-page-title-main">Cygwin</span> Unix subsystem for Windows machines

Cygwin is a Unix-like environment and command-line interface for Microsoft Windows. Cygwin's purpose is expressed in its motto: "Get that Linux feeling – on Windows".

<span class="mw-page-title-main">Shell script</span> Script written for the shell, or command line interpreter, of an operating system

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 scripting 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.

<span class="mw-page-title-main">Unix shell</span> Command-line interpreter for Unix operating system

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.

<span class="mw-page-title-main">Bourne shell</span> Command-line interpreter for operating systems

The Bourne shell (sh) is a shell command-line interpreter for computer operating systems.

<span class="mw-page-title-main">C shell</span> Unix shell

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 each process. It is sometimes called the current working directory (CWD), e.g. the BSD getcwd function, or just current directory. When the 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 software development, Make is a build automation tool that builds executable programs and libraries from source code by reading files called makefiles which specify how to derive the target program. Though integrated development environments and language-specific compiler features can also be used to manage a build process, Make remains widely used, especially in Unix and Unix-like operating systems.

pwd Directory information command on various operating systems

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.

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.

<span class="mw-page-title-main">Pipeline (Unix)</span> Mechanism for inter-process communication using message passing

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. The concept of pipelines was championed by Douglas McIlroy at Unix's ancestral home of Bell Labs, during the development of Unix, shaping its toolbox philosophy. It is named by analogy to a physical pipeline. A key feature of these pipelines is their "hiding of internals". This in turn allows for more clarity and simplicity in the system.

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.

<span class="mw-page-title-main">Comparison of command shells</span>

A command shell is a command-line interface to interact with and manipulate a computer's operating system.

mount (Unix) Unix command to mount a filesystem

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.

alias (command) Command in various command line interpreters

In computing, alias is a command in various command-line interpreters (shells), which enables a replacement of a word by another string. It is mainly used for abbreviating a system command, or for adding default arguments to a regularly used command. alias is available in Unix shells, AmigaDOS, 4DOS/4NT, KolibriOS, Windows PowerShell, ReactOS, and the EFI shell. Aliasing functionality in the MS-DOS and Microsoft Windows operating systems is provided by the DOSKey command-line utility.

In computing, a shebang is the character sequence consisting of the characters number sign and exclamation mark at the beginning of a script. It is also called sharp-exclamation, sha-bang, hashbang, pound-bang, or hash-pling.

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.

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.

References

  1. "fish shell team members". GitHub.com. Retrieved 2021-07-28.
  2. "released March 25, 2023". 25 March 2023. Retrieved 4 June 2023.
  3. fishshell.com License for fish
  4. Liljencrantz, Axel (2005-05-17). "Fish - A user-friendly shell". Linux Weekly News . Retrieved 2010-03-24.
  5. "Fish docs: design" . Retrieved 2021-04-09.
  6. Linux.com. CLI Magic: Enhancing the shell with fish. Retrieved 2010-03-24.
  7. Paul, Ryan (19 December 2005). "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.
  8. "Bash Pitfalls" . Retrieved 2016-07-10. 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.
  9. "RFC: Add binding to expand/evaluate tokens on commandline". GitHub . 2013-05-16. Retrieved 2021-04-09.
  10. "printf does not support \e". fish issues. 11 Jul 2013. Retrieved 24 March 2016.