Friendly interactive shell
Encyclopedia
The friendly interactive shell (fish) is a Unix shell
Unix shell
A Unix shell is a command-line interpreter or shell that provides a traditional user interface for the Unix operating system and for Unix-like systems...

 that focuses on interactive use, discoverability, and user friendliness. 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.

Released in 2005 under the terms of the GNU General Public License
GNU General Public License
The GNU General Public License is the most widely used free software license, originally written by Richard Stallman for the GNU Project....

, fish is free software
Free software
Free software, software libre or libre software is software that can be used, studied, and modified without restriction, and which can be copied and redistributed in modified or unmodified form either without restriction, or with restrictions that only ensure that further recipients can also do...

 according to the Free Software Foundation
Free Software Foundation
The Free Software Foundation is a non-profit corporation founded by Richard Stallman on 4 October 1985 to support the free software movement, a copyleft-based movement which aims to promote the universal freedom to create, distribute and modify computer software...

.

Highlights

fish features a user-friendly and powerful tab-completion
Command line completion
Command line completion is a common feature of command line interpreters, in which the program automatically fills in partially typed commands....

, including descriptions of every completion, tab-completion of strings with wildcard
Wildcard character
-Telecommunication:In telecommunications, a wildcard character is a character that may be substituted for any of a defined subset of all possible characters....

s, and many command specific completions. It also features an extensive and discoverable help system. A special help command gives access to all the fish documentation in the user's web browser
Web browser
A web browser is a software application for retrieving, presenting, and traversing information resources on the World Wide Web. An information resource is identified by a Uniform Resource Identifier and may be a web page, image, video, or other piece of content...

.

Syntax

The fish syntax is slightly different from other shell script
Shell script
A shell script is a script written for the shell, or command line interpreter, of an operating system. It is often considered a simple domain-specific programming language...

 languages. These changes were made to make the language more powerful as well as to make the language small and easy to learn. One obvious difference between fish and other command-line interpreters like bash is that the contents of a variable is not subject to token separation, meaning that there is rarely a need to enclose variable dereferences within quotes.

  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 (pwd)
> 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 -e 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. Semicolons work like newlines:

> for i in *.jpg; convert $i (basename $i .jpg).png; end
  1. but the multi-line form is comfortable to use because
  2. fish supports multi-line history and editing.

  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.

> cat /etc/passwd | while read line
set arr (echo $line|tr : \n)
echo $arr[5]
end


One important difference between fish and other shells
Shell (computing)
A shell is a piece of software that provides an interface for users of an operating system which provides access to the services of a kernel. However, the term is also applied very loosely to applications and may include any software that is "built around" a particular component, such as web...

 is the lack of subshells. Many tasks like pipeline
Pipeline (software)
In software engineering, a pipeline consists of a chain of processing elements , arranged so that the output of each element is the input of the next. Usually some amount of buffering is provided between consecutive elements...

s, functions
Subroutine
In computer science, a subroutine is a portion of code within a larger program that performs a specific task and is relatively independent of the remaining code....

 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 most other shells, since the 'read' builtin
# will run in its own subshell. fish and zsh work as expected.
> 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.

> 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 -U EDITOR emacs

# This command will make the current working directory part of the fish
# prompt turn blue on all running fish instances.
> set -U fish_color_cwd blue

Other features

  • Advanced tab completion.
  • Syntax highlighting
    Syntax highlighting
    Syntax highlighting is a feature of some text editors that display text—especially source code—in different colors and fonts according to the category of terms. This feature eases writing in a structured language such as a programming language or a markup language as both structures and...

     with extensive error checking.
  • Support for the X
    X Window System
    The X window system is a computer software system and network protocol that provides a basis for graphical user interfaces and rich input device capability for networked computers...

     clipboard
    Clipboard (software)
    The clipboard is a software facility that can be used for short-term data storage and/or data transfer between documents or applications, via copy and paste operations...

    .
  • Smart terminal
    Computer terminal
    A computer terminal is an electronic or electromechanical hardware device that is used for entering data into, and displaying data from, a computer or a computing system...

     handling based on terminfo
    Terminfo
    Terminfo is a library and database that enables programs to use display terminals in a device-independent manner. This library has its origins in the UNIX System III operating system....

    .
  • Searchable command history
    Command History
    Command history is a feature in many operating system shells, computer algebra programs, and other software that allows the user to recall, edit and rerun previous commands....

    .

External links

  • Project home page
  • Community version project page
  • fish on SourceForge.net
    SourceForge.net
    SourceForge is a web-based source code repository. It acts as a centralized location for software developers to control and manage open source software development. The website runs a version of SourceForge Enterprise Edition, forked from the last open-source version available...

  • An introductory article about fish
  • Fish: the friendly interactive shell (an in-depth look at fish) at Ars Technica
    Ars Technica
    Ars Technica is a technology news and information website created by Ken Fisher and Jon Stokes in 1998. It publishes news, reviews and guides on issues such as computer hardware and software, science, technology policy, and video games. Ars Technica is known for its features, long articles that go...

The source of this article is wikipedia, the free encyclopedia.  The text of this article is licensed under the GFDL.
 
x
OK