Fish：一个友好的交互式 Shell
Fish shell Bash
![Fish — A Friendly Interactive Shell](/data/attachment/album/202004/03/214403wr6sd4fdld6gul6r.jpg)


你是否正在寻找 bash 的替代品？你是否在寻找更人性化的东西？不用再看了，因为你刚发现了 fish！


Fish（友好的交互式 shell）是一个智能且用户友好的命令行 shell，可在 Linux、MacOS 和其他操作系统上运行。可以将其用于终端的日常工作和脚本编写。用 fish 编写的脚本比相同的 bash 版本具有更少的神秘性。


### Fish 的用户友好功能


* **建议**：Fish 会提示你之前写过的命令。当经常输入相同命令时，这样可以提高生产率。
* **健全的脚本能力**：Fish 避免使用那些隐秘字符。这提供了更清晰和更友好的语法。
* **基于手册页的补全**：Fish 会根据命令的手册页自动补全参数。
* **语法高亮**：Fish 会高亮显示命令语法以使其在视觉上友好。


### 安装


#### Fedora 工作站


使用 `dnf` 命令安装 fish：



```
$ sudo dnf install fish
```

安装 `util-linux-user` 包，然后使用适当的参数运行 `chsh`（更改 shell 程序）命令，将 fish 设置为默认 shell 程序：



```
$ sudo dnf install util-linux-user
$ chsh -s /usr/bin/fish
```

你需要注销然后重新登录，更改才能生效。


#### Fedora Silverblue


由于它不是 GUI 应用，因此你需要使用 `rpm-ostree` 将其加到层内。使用以下命令在 Fedora Silverblue 上安装 fish：



```
$ rpm-ostree install fish
```

在 Fedora Silverblue 上，你需要重启 PC 才能切换到新的 ostree 镜像。


如果你想在 Fedora Silverblue 用 fish 作为主要 shell，最简单的方法是更新 `/etc/passwd` 文件。找到你的用户，并将 `/bin/bash` 更改为 `/usr/bin/fish`。


你需要 [root 权限](https://fedoramagazine.org/howto-use-sudo/)来编辑 `/etc/passwd` 文件。另外，你需要注销并重新登录才能使更改生效。


### 配置


fish 的用户配置文件在 `~/.config/fish/config.fish`。要更改所有用户的配置，请编辑 `/etc/fish/config.fish`。


用户配置文件必须手动创建。安装脚本不会创建 `~/.config/fish/config.fish`。


以下是两个个配置示例以及它们的 bash 等效项，以帮助你入门：


#### 创建别名


* `~/.bashrc`：`alias ll='ls -lh'`
* `~/.config/fish/config.fish`: `alias ll='ls -lh'`


#### 设置环境变量


* `~/.bashrc`：`export PATH=$PATH:~/bin`
* `~/.config/fish/config.fish`：`set -gx PATH $PATH ~/bin`


### 使用 fish 工作


将 fish 配置为默认 shell 程序后，命令提示符将类似于下图所示。如果尚未将 fish 配置为默认 shell，只需运行 `fish` 命令以在当前终端会话中启动。


![](/data/attachment/album/202004/03/214403c2xxq31x3o1o2192.png)


在你开始输入命令时，你会注意到语法高亮显示：


![](/data/attachment/album/202004/03/214404av4mcisn1cpymium.png)


很酷，不是吗？?


你还将在输入时看到建议的命令。例如，再次开始输入上一个命令：


![](/data/attachment/album/202004/03/214404d2tslhgmlhxittdy.png)


注意输入时出现的灰色文本。灰色文本显示建议之前编写的命令。要自动补全，只需按 `CTRL+F`。


通过输入连接号（`–`）然后使用 `TAB` 键，它会根据前面命令的手册页获取参数建议：


![](/data/attachment/album/202004/03/214404uz8klm8uxtulcq18.png)


如果你按一次 `TAB`，它将显示前几个建议（或所有建议，如果只有少量参数可用）。如果再次按 `TAB`，它将显示所有建议。如果连续三次按 `TAB`，它将切换到交互模式，你可以使用箭头键选择一个参数。


除此之外，fish 的工作与大多数其他 shell 相似。其他差异已经写在文档中。因此，找到你可能感兴趣的其他功能应该不难。


### 让 fish 变得更强大


使用 [powerline](https://github.com/oh-my-fish/theme-bobthefish) 使 fish 变得更强大。Powerline 可以为 fish 的界面添加命令执行时间、彩色化 git 状态、当前 git 分支等。


在安装 powerline 之前，你必须先安装 [Oh My Fish](https://github.com/oh-my-fish/oh-my-fish)。Oh My Fish 扩展了 fish 的核心基础架构，以支持安装其他插件。安装 Oh My Fish 的最简单方法是使用 `curl` 命令：



```
> curl -L https://get.oh-my.fish | fish
```

如果你不想直接将安装命令管道传给 `fish`，请参见 Oh My Fish 的 [README](https://github.com/oh-my-fish/oh-my-fish/blob/master/README.md#installation) 的安装部分，以了解其他安装方法。


Fish 的 powerline 插件是 [bobthefish](https://github.com/oh-my-fish/theme-bobthefish)。Bobthefish 需要 `powerline-fonts` 包。


在 Fedora 工作站上：



```
> sudo dnf install powerline-fonts
```

在 Fedora Silverblue 上：



```
> rpm-ostree install powerline-fonts
```

在 Fedora Silverblue 上，你必须重启以完成字体的安装。


安装 `powerline-fonts` 之后，安装 `bobthefish`：



```
> omf install bobthefish
```

现在你可以通过 powerline 体验 fish 的全部奇妙之处：


![](/data/attachment/album/202004/03/214405ql4e7i70vbp5lurd.png)


### 更多资源


查看这些网页，了解更多 fish 内容：


* [官网](https://fishshell.com/)
* [文档](https://fishshell.com/docs/current/index.html)
* [教程](https://fishshell.com/docs/current/tutorial.html)
* [常见问题](https://fishshell.com/docs/current/faq.html)
* [在线体验](https://rootnroll.com/d/fish-shell/)
* [邮件列表](https://sourceforge.net/projects/fish/lists/fish-users)
* [GitHub](https://github.com/fish-shell/fish-shell/)




---


via: <https://fedoramagazine.org/fish-a-friendly-interactive-shell/>


作者：[Michal Konečný](https://fedoramagazine.org/author/zlopez/) 选题：[lujun9972](https://github.com/lujun9972) 译者：[geekpi](https://github.com/geekpi) 校对：[wxy](https://github.com/wxy)


本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译，[Linux中国](https://linux.cn/) 荣誉推出


OK
Are you looking for an alternative to bash? Are you looking for something more user-friendly? Then look no further because you just found the golden fish!

Fish (friendly interactive shell) is a smart and user-friendly command line shell that works on Linux, MacOS, and other operating systems. Use it for everyday work in your terminal and for scripting. Scripts written in fish are less cryptic than their equivalent bash versions.

## Fish’s user-friendly features

**Suggestions**

Fish will suggest commands that you have written before. This boosts productivity when typing same commands often.**Sane scripting**

Fish avoids using cryptic characters. This provides a clearer and friendlier syntax.**Completion based on man pages**

Fish will autocomplete parameters based on the the command’s man page.**Syntax highlighting**

Fish will highlight command syntax to make it visually friendly.

## Installation

### Fedora Workstation

Use the *dnf* command to install fish:

$ sudo dnf install fish

Make fish your default shell by installing the *util-linux-user* package and then running the *chsh* (change shell) command with the appropriate parameters:

$ sudo dnf install util-linux-user $ chsh -s /usr/bin/fish

You will need to log out and back in for this change to take effect.

### Fedora Silverblue

Because this is not GUI application, you will need to layer it using *rpm-ostree*. Use the following command to install fish on Fedora Silverblue:

$ rpm-ostree install fish

On Fedora Silverblue you will need to reboot your PC to switch to the new ostree image.

If you want to make fish your main shell on Fedora Silverblue, the easiest way is to update the */etc/passwd* file. Find your user and change */bin/bash* to */usr/bin/fish*.

You will need [root privileges](https://fedoramagazine.org/howto-use-sudo/) to edit the */etc/passwd* file. Also you will need to log out and back in for this change to take effect.

## Configuration

The per-user configuration file for fish is *~/.config/fish/config.fish*. To make configuration changes for all users, edit */etc/fish/config.fish* instead.

The per-user configuration file must be created manually. The installation scripts will not create *~/.config/fish/config.fish*.

Here are a couple configuration examples shown alongside their bash equivalents to get you started:

### Creating aliases

*~/.bashrc*:alias ll='ls -lh'*~/.config/fish/config.fish*:alias ll='ls -lh'

### Setting environment variables

*~/.bashrc*:export PATH=$PATH:~/bin*~/.config/fish/config.fish*:set -gx PATH $PATH ~/bin

## Working with fish

When fish is configured as your default shell, the command prompt will look similar to what is shown in the below image. If you haven’t configured fish to be your default shell, just run the *fish* command to start it in your current terminal session.

![](https://fedoramagazine.org/wp-content/uploads/2020/03/Screenshot-from-2020-03-03-14-00-35.png)

As you start typing commands, you will notice the syntax highlighting:

![](https://fedoramagazine.org/wp-content/uploads/2020/03/Screenshot-from-2020-03-03-14-19-24.png)

Cool, isn’t it? 🙂

You will also see commands being suggested as you type. For example, start typing the previous command a second time:

![](https://fedoramagazine.org/wp-content/uploads/2020/03/Screenshot-from-2020-03-03-14-25-31.png)

Notice the gray text that appears as you type. The gray text is fish suggesting the command you wrote before. To autocomplete it, just press **CTRL+F**.

Get argument suggestions based on the preceding command’s man page by typing a dash (**–**) and then the **TAB** key:

![](https://fedoramagazine.org/wp-content/uploads/2020/03/Screenshot-from-2020-03-03-14-58-07.png)

If you press **TAB** once, it will show you the first few suggestions (or every suggestion, if there are only a few arguments available). If you press **TAB** a second time, it will show you all suggestions. If you press **TAB** three times consecutively, it will switch to interactive mode and you can select an argument using the arrow keys.

Otherwise, fish works similar to most other shells. The remaining differences are well documented. So it shouldn’t be difficult to find other features that you may be interested in.

## Make fish even more powerful

Make the fish even more powerful with [powerline](https://github.com/oh-my-fish/theme-bobthefish). Powerline adds command execution time, colored git status, current git branch and much more to fish’s interface.

Before installing powerline for fish, you must install [Oh My Fish](https://github.com/oh-my-fish/oh-my-fish). Oh My Fish extends fish’s core infrastructure to enable the installation of additional plugins. The easiest way to install Oh My Fish is to use the *curl* command:

> curl -L https://get.oh-my.fish | fish

If you don’t want to pipe the installation commands directly to *curl*, see the installation section of Oh My Fish’s [README](https://github.com/oh-my-fish/oh-my-fish/blob/master/README.md#installation) for alternative installation methods.

Fish’s powerline plugin is [bobthefish](https://github.com/oh-my-fish/theme-bobthefish). Bobthefish requires the *powerline-fonts* package.

**On Fedora Workstation**:

> sudo dnf install powerline-fonts

**On Fedora Silverblue**:

> rpm-ostree install powerline-fonts

On Fedora Silverblue you will have to reboot to complete the installation of the fonts.

After you have installed the *powerline-fonts* package, install *bobthefish*:

> omf install bobthefish

Now you can experience the full awesomeness of fish with powerline:

![](https://fedoramagazine.org/wp-content/uploads/2020/03/Screenshot-from-2020-03-03-15-38-07.png)

## Additional resources

Check out these web pages to learn even more about fish:

## Martin Sehnoutka

Fish is really awesome :), but is it safe to modify /etc/passwd with a shell that is incompatible with bash? The last time I tried something broke so I decided to only configure my terminal emulator to start fish instead of the default shell.

## Sebastiaan Franken

The shell doesn’t edit. The program (chsh) does, the only thing the shell does is invoke that program, nothing more.

If something broke in the past is probably bad luck, or a bad program. Chsh is old as nails though, so you can safely assume it’ll do it’s job without killing your install.

## Eduard Lucena

I have 2 questions:

1.- How do I install it without enabling modules?

2.- What is ofm?

## xr

omf is OhMyFish’s plugin manager.

## Michal Konečný

Look at the official site, there is guide for installation from source or directly from git.

omf is OhMyFish, which I described earlier in the article.

## ee3w

instaling trought runing bash are extremely dangerous!

curl -L https://web | bash

## Michal Konečný

You can use other installation options, just look at the OhMyFish repo.

## AsciiWolf

Do not edit /etc/passwd manually, it is always a bad idea that can break your system! You can use lchsh on Silverblue.

## Michal Konečný

Thanks for info. I didn’t knew lchsh exists till now.

## someSay

Fish is great, I use it for years 🙂 Some additions to that article:

dnf installs fish from modularity by default, which is bad because it only contains version 3.0. The fedora-updates repo already contains the latest and greatest fish 3.1 which comes with a lots of improvements. No need to build from source. Better do:

dnf module disable fish

dnf distro-sync fish

One can create aliases, e.g. for ll=’ls -lh’ but this is not necessary in this case. Fish has an ll wrapper function shipped by default for this common scenario. One of the things that makes fish great 🙂

While the ability of parsing man pages for completions is cool, the cd completion isn’t derived from the man page. It’s an explicitly written completion script among many others. Have a look at different completion scripts written for fish. They are much more readable than for bash.

For starters, I highly recommend going through the tutorial linked in the article. It explains further niceties.

## Rafael

Hi,

first everything looks very nice with colorful syntax, but sadly, now I have to put expressions like URLs or with blank separated words into “-Quotes.

I am used not to do this since over 10 years.

Is it possible to use fish but without using “-Quotes, like in bash?

Thanks