在 Fedora 系统上设置 zsh
zsh shell
![](/data/attachment/album/201806/09/234047chxm6e9nisfdbvvf.jpg)


对于一些人来说，终端可能会很吓人。但终端不仅仅是一个输入的黑屏。它通常运行一个 shell（外壳），如此称呼的原因是它围绕着内核。shell 是一个基于文本的界面，可让你在系统上运行命令。它有时也被称为<ruby> 命令行解释器 <rt>  command line interpreter </rt></ruby>（CLI）。与大多数 Linux 发行版一样，Fedora 带有 bash 作为默认 shell。但是，它不是唯一可用的 shell，你可以安装其他的 shell。本文重点介绍 Z Shell （即 zsh）。


Bash 是对 UNIX 中提供的旧式 Bourne shell（sh）的重写（LCTT 译注：Bourne Again SHell）。zsh 视图通过更好的交互以比 bash 更友善。它的一些有用功能是：


* 可编程的命令行补全 \* 在运行的 shell 会话之间共享命令历史 \* 拼写纠正 \* 可加载模块 \* 交互式选择文件和文件夹


zsh 在 Fedora 仓库中存在。要安装，请运行以下命令：



```
$ sudo dnf install zsh

```

### 使用 zsh


要开始使用它，只需输入 `zsh`，新的 shell 在第一次运行时显示向导。该向导可帮助你配置初始功能，如历史记录行为和自动补全。或者你可以选择保持 [rc 文件](https://en.wikipedia.org/wiki/Configuration_file) 为空：


![zsh First Run Wizzard](/data/attachment/album/201806/09/234047gku3vovll47okfhj.gif)


如果输入 `1`，则启动配置向导。其他选项立即启动 shell。


请注意，用户提示符是 `％` 而不是与 bash 的 `$`。这里的一个重要功能是自动补全功能，它允许你使用 `Tab` 键在文件和目录之间移动，非常类似于菜单：


![zsh cd Feature](/data/attachment/album/201806/09/234048f537y5cb7lwlbm77.gif)


另一个有趣的功能是拼写纠正，这有助于在混合大小写的情况下输入文件名：


![zsh Auto Completion](/data/attachment/album/201806/09/234048eo5s85zosx23ccns.gif)


使用 zsh 成为你的默认 shell
-------------------


zsh 提供了很多插件，如 zsh-syntax-highlighting 和著名的 “Oh my zsh”（[在此查看其页面](http://ohmyz.sh/)）。也许你希望将其设为默认 shell，以便在你在开始会话或打开终端时运行。为此，请使用 `chsh`（“更改 shell”）命令：



```
$ chsh -s $(which zsh)

```

这个命令告诉你的系统你要设置（`-s`）默认 shell 为该 shell 的正确位置（`which zsh`）。


图片来自 [Flickr](https://www.flickr.com/photos/katerha/34714051013/) 由 [Kate Ter Haar](https://www.flickr.com/photos/katerha/) 提供（CC BY-SA）。




---


via: <https://fedoramagazine.org/set-zsh-fedora-system/>


作者：[Eduard Lucena](https://fedoramagazine.org/author/x3mboy/) 选题：[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
For some people, the terminal can be scary. But a terminal is more than just a black screen to type in. It usually runs a *shell*, so called because it wraps around the kernel. The shell is a text-based interface that lets you run commands on the system. It’s also sometimes called a command line interpreter or *CLI*. Fedora, like most Linux distributions, comes with *bash* as the default shell. However, it isn’t the only shell available; several other shells can be installed. This article focuses on the Z Shell, or *zsh*.


Bash is a rewrite of the old Bourne shell (*sh*) that shipped in UNIX. Zsh is intended to be friendlier than bash, through better interaction. Some of its useful features are:

- Programmable command line completion
- Shared command history between running shell sessions
- Spelling correction
- Loadable modules
- Interactive selection of files and folders

Zsh is available in the Fedora repositories. To install, run this command:

$ sudo dnf install zsh

### Using zsh

To start using it, just type *zsh* and the new shell prompts you with a first run wizard. This wizard helps you configure initial features, like history behavior and auto-completion. Or you can opt to keep the [rc file](https://en.wikipedia.org/wiki/Configuration_file) empty:

![zsh First Run Wizzard](https://fedoramagazine.org/wp-content/uploads/2017/12/zshFirstRun.gif)

First-run wizard

If you type 1 the configuration wizard starts. The other options launch the shell immediately.

Note that the user prompt is **%** and not **$** as with bash. A significant feature here is the auto-completion that allows you to move among files and directories with the *Tab* key, much like a menu:

![zsh cd Feature](https://fedoramagazine.org/wp-content/uploads/2017/12/zshChangingFeature-1.gif)

Using the auto-completion feature with the *cd* command

Another interesting feature is spelling correction, which helps when writing filenames with mixed cases:

![zsh Auto Completion](https://fedoramagazine.org/wp-content/uploads/2017/12/zshAutoCompletion.gif)

Auto completion performing spelling correction

## Making zsh your default shell

Zsh offers a lot of plugins, like zsh-syntax-highlighting, and the famous “Oh my zsh” ([check out its page here](http://ohmyz.sh/)). You might want to make it the default, so it runs whenever you start a session or open a terminal. To do this, use the *chsh* (“change shell”) command:

$ chsh -s $(which zsh)

This command tells your system that you want to set (*-s*) your default shell to the correct location of the shell (*which zsh*).

Photo by [Kate Ter Haar](https://www.flickr.com/photos/katerha/) from [Flickr](https://www.flickr.com/photos/katerha/34714051013/) (CC BY-SA).

## Creak

It’s when I discovered Oh-My-Zsh that I definitely changed from bash to zsh, and never looked back 😉

## Isaac

Using with “antigen” is very useful!

## Ivan Augusto

Really good tip, ZSH is much powerful! 😀

A full guide to have a really, really powerful terminal using it: https://medium.com/@ivanaugustobd/seu-terminal-pode-ser-muito-muito-mais-produtivo-3159c8ef77b2 (it’s in Brazilian Portuguese, but can be easily translated using your favorite tool)

## Dirk

I use ZHS with antigen and various activated plug-ins on Fedora for years now. And I don’t want to miss it.

## Leslie Satenstein

Whats wrong with bash?

## Creak

Bash is very good, but quite limited in its features. I thought bash was largely enough for me (I’m not a hardcore shell user), but I tried zsh, just to see what it is.

What really changed my mind about zsh is when I configured it with Oh-My-Zsh (or Antigen, which is an attempt to make Oh-My-Zsh modular). Some day-to-day features are so useful, I just get frustrated when I go back to bash 😀

## Brian Vaughan

Nothing. ZSH just has some syntactic sugar some may like. I played with it for a while, and used Oh-My-Zsh, which has some cute tricks for the command line.

But, I’d never consider installing Oh-My-Zsh on a production system. And, I never got around to learning how to use ZSH’s unique features for scripting; it occurred to me that it didn’t make a lot of sense to do so. BASH is ubiquitous, and if BASH isn’t adequate to a task, I’d rather use Perl or Python, which are nearly as ubiquitous.

## Sudhir

What happens when you run bash or the usual commands on Zsh? I suppose ~/.bashrc, scripts, etc. continues to work as usual.

## Eduard Lucena

If you run bash, you just goes to bash through the normal process that include reading .bashrc .bash_profile and all other files needed to run bash. The commands just call programs, so yes, they work as usual: ls, cd, mv, cp, ln and everything works as usual, regular expression matching is a little different, the work with wildcards is also a little different, and the scripting is totally different. The scripts made for bash including th shabang works as usual.

## Brian Vaughan

Zsh, like Bash, is POSIX-compliant; the command line functions basically the same way, scripts that stick to POSIX features only should work with either shell, and a lot of Bash’s special features are also present in Zsh.

Normally, a shell script starts with a shebang, as in “#!/bin/bash” or “#!/bin/zsh”, which specifies the script interpreter. So a properly written script would run correctly, regardless of which shell the user is using at the command line.

You can launch an instance of Bash from within an instance of Zsh, or vice versa, just as you can launch a new instance of Bash from within an instance of Bash.

~/.bashrc, and the other related dotfiles that begin with “.bash”, are specific to Bash. Zsh uses ~/.zshrc . There are other dotfiles that are independent of a particular shell, e.g., .profile.

## Creak

Zsh is more for your day-to-day use of the command line. It brings useful features that bash simply doesn’t have (e.g. the exceptional completion system).

But if you’re writing a script for general purpose, always use a shebang to sh (i.e. #!/bin/sh) at the top of your script. Doing so, you’re scripts will run in a strong, cross-compatible shell environment.

## bladekp

Here you can find some nice startup confings for your ZSH: https://bitbucket.org/Vifon/zsh-config