请输入您要查询的百科知识:

 

词条 Friendly interactive shell
释义

  1. Highlights

  2. Syntax

      No implicit subshell    Variable assignment example  

  3. Helpful error messages

  4. Universal variables

  5. Other features

  6. bash/fish Translation table

  7. See also

  8. References

  9. External links

{{lowercase}}{{Infobox software
| name = fish
| logo = Fish shell logo ascii.png
| screenshot = Friendlyinteractiveshell.png
| screenshot size = 280px
| caption = The friendly interactive shell
| author = Axel Liljencrantz
| developer = ridiculousfish, siteshwar, JanKanis[1]
| released = {{release date and age|df=yes|2005|02|13}}
| latest release version = 3.0.2
| latest release date = {{Start date and age|2019|02|19|df=yes}}[2]
| operating system = Unix-like
| genre = 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 those with a longer history (i.e. most other Unix shells) or those formulated as function-compatible replacements for the aforementioned (e.g. zsh, the Falstad shell). 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 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 {{keypress|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 {{keypress|tab}} for a tab completion instead. Tab-completion is feature-rich, 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]

  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 --erase 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 : \)      echo $arr[5]  end

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 needs no 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.[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.

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

  • 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:
with word splitting and glob interpretation

or

or

deliberately omitted}} Identified as a primary cause of bugs in posix compatible shell languages
variable expansion:
scalar
deliberately omitted}} Every variable is an array
variable expansion:
array
Quoting not necessary to suppress word splitting and glob interpretation. Instead, quoting signifies serialization.
variable expansion:
as a space separated string
history completion Ctrl|R}}implicit}}
history substitution deliberately omitted}} Not discoverable
explicit subshell no equivalent}}
command substitution
process substitution

<(expression)

Command, not syntax
logical operators cmd && echo FAIL echo OK

not command

and echo FAIL

or echo OK

variable assignment

set var value

string processing:
replace
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 Options discoverable via tab completion
function-local variable by default}}
scope-local variable no equivalent}}
remove variable
check if a variable exists
array initialization

set var a b c

Every variable is an array
array iteration

for i in "${var[@]}"; do

done

||

for i in $var

end

argument vector:
all arguments
argument vector:
indexing
argument vector:
length
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 env LANG=C.UTF-8 python3 env LANG=C.UTF-8 python3 works in any shell, as env is a standalone program.
arithmetic expr 10 / 3 works in any shell, as expr is a standalone program.
escape sequence $'\\e' printf '\\e' works in both shells; their printf builtins are both compatible with the GNU printf standalone program.[8]
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

{{Portal|Free and open-source software}}
  • Comparison of command shells

References

1. ^{{cite web| url = https://github.com/fish-shell?tab=members| title = fish shell team members | publisher = GitHub.com| date = | accessdate = 2013-05-21}}
2. ^[https://github.com/fish-shell/fish-shell/releases/tag/3.0.2] Release Notes for fish 3.0.2
3. ^fishshell.com License for fish
4. ^[https://lwn.net/Articles/136232/ Linux Weekly News]. Fish - A user-friendly shell. Retrieved 2010-03-24.
5. ^[https://www.linux.com/news/cli-magic-enhancing-shell-fish Linux.com]. CLI Magic: Enhancing the shell with fish. Retrieved 2010-03-24.
6. ^{{cite web|last1=Paul|first1=Ryan|title=An in-depth look at fish: the friendly interactive shell|url=https://arstechnica.com/information-technology/2005/12/linux-20051218/2/|website=Ars Technica|accessdate=10 March 2015|quote=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.}}
7. ^[https://web.archive.org/web/20120719015847/http://www.handlewithlinux.com/afraid-of-the-linux-shell-try-fish Handle With Linux]. Afraid of the command line? Try fish. Archived from the original on 2012-07-19.
8. ^{{cite web|title=printf does not support \\e|url=https://github.com/fish-shell/fish-shell/issues/910|website=fish issues|accessdate=24 March 2016|date=11 Jul 2013}}

}}

External links

  • {{Official website}} – containing documentation and downloads
  • [https://github.com/fish-shell/fish-shell fish] on GitHub (active)
  • fish on Gitorious (obsolete)
  • fish on SourceForge (obsolete)
  • [https://lists.sourceforge.net/lists/listinfo/fish-users Fish-users] – general discussion list for fish users
  • [https://github.com/fish-shell/fish-shell/wiki/Shell-Translation-Dictionary Shell Translation Dictionary] - another Bash/Fish translation table
{{Unix Shells}}{{DEFAULTSORT:Friendly Interactive Shell}}

3 : Unix shells|Scripting languages|Free software programmed in C

随便看

 

开放百科全书收录14589846条英语、德语、日语等多语种百科知识,基本涵盖了大多数领域的百科知识,是一部内容自由、开放的电子版国际百科全书。

 

Copyright © 2023 OENC.NET All Rights Reserved
京ICP备2021023879号 更新时间:2024/11/11 19:35:01