各种累 发表于 2016-10-1 14:55:03
When we talk about UNIX based programming, it's usually about the shells, terminals, and the command line interfaces. The most prevalent shell in this regard is Bashbut there are other variants available and used widely as well, like Zsh or the Z shell.
您需要 登录 才可以下载或查看，没有帐号？立即注册
In this article, we'll attempt to draw the line between the two shells and show the differences so you can get a sense of why you might use one or the other. But first, in the following sections we'll introduce both shells before we compare them together.
Zsh, or Z shell, was first released by Paul Falstad back in 1990 when he was still a student at Princeton University. Z shell is included in many operating systems, including Mac OS (although it isn't the default that's actually used).
Much like Bash, Z shell can basically be seen as an extended version of the Bourne shell, and does contain a lot of the same features as Bash, which you'll probably notice in the sections below. You may also notice that it pretty closely resembles the Korn shell as well. Some of the features that are worth mentioning include (but are not limited to):
There are a lotmore features than what we've shown here, but at least this gives you an idea as to how shells can be different.
- File globbing
- Spelling correction
- Directory aliases (much like ~or ..)
- Loadable modules, like socket controls or an FTP client
- Compatibility modes: e.g. You can use /bin/bashas a drop-in replacement for Bash
- Startup/shutdown scripts via zshenv, zprofile, zshrc, zlogin, and zlogout
- gitcommand completion
- Path expansion: e.g. Enter cd /u/lo/b, press tab, and it will be completed to cd /usr/local/binsince it is the only matching pattern
The Bash shell (also known as the "Bourne-again shell") was also released around the same period as the Z shell (in 1989) and Brian Fox is regarded as the creator behind it. It was initially written as a replacement for the Bourne shell. For many years it has shipped as the default shell for GNU, most Linux distributions, and Mac OS X (version 10.3+). Like a true predecessor should, Bash is capable of executing all of the Bourne shell commands without a problem.
There are quite a few features that the Bash shell has and some of the lesser-known ones include:
If you want to learn more, you can see a much larger list of Bash-specific features here.
- Insert the last parameter(s) of the preceding command in your current command using Alt+ .
- You can keep a process running even after logging out. To do so, use the command disown -h <pid>where you will have to place the process ID (PID) of the program instead of <pid>
- Execute the previous command again, but this time running with sudousing the command sudo !!( !!is shorthand for 'the previous command')
- Perform a reverse incremental search using the Ctrl+ Rkeys
- Press tab twice and you will see the list of completions for the word that you have just typed or are typing
- When executing a script with bash, use the -xoption to output the script contents as it's being executed
Comparing Z shell and Bash
Now that we've give you a brief introduction to both of the shells, let's see how they hold up when actually compared and contrasted together.
The first thing to look at (and one of the more significant aspects, in my opinion) is prevalence and popularity of the shell. While the Z shell has its fair share of users throughout the developer community, it's usually safer to write your scripts for Bash since there is a much larger group of people that will be able to run those scripts.
The importance of adoption holds true for the public resources and documentation as well. Thanks to its large community, Bash has quite a few more resources out there to help you learn how to use it.
So, if you are planning on writing a script that you want many developers to easily be able to run then I'd recommend that you go with Bash. However, this shouldn't stop you from using Z shell if your end goal is more suited to Z shell. Finding the right solution to a problem is much more important than just using what's popular, so keep that in mind as well.
Although Bash is much more popular, that doesn't mean Z shell is without any useful features of its own. It's actually heavily praised for its interactive use, because it's more customizable than Bash. For example, the prompts are more flexible. It can display a prompt on the left andanother on the right side of the screen, much like vim's split screen. The auto-completion is also more customizable and is actually faster than Bash's.
To give you a better sense of what kind of features Z shell has, here is a list of things that you will have access to when using Z shell over Bash:
There are also a bunch of features that are present in the Bash terminal but are absent from almost all of the other shells. Here are a few of them as well:
- The built-in zmvcommand can help you do massive file/directory renames. e.g. to append '.txt' to every file name run zmv –C '(*)(#q.)' '$1.txt'
- The zcalcutility is a great command-line calculator which is a convenient way to do a quick calculation without leaving the terminal. Load it with autoload -Uz zcalcand run with zcalc
- The zparseoptscommand is a one-liner that lets you to parse complex options that are provided to your script
- The autopushdcommand helps you do popdafter you use cdto go back to your previous directory
- Floating point support (which Bash suprisingly does nothave)
- Support for hash data structures
It's difficult to say which shell is actually better. It all really depends on your own preferences and what you actually want to do with the shell. In the case of Bash vs Z shell, neither is really better than the other.
- The –norccommand-line option, which allows the user to proceed with the shell initialization withoutreading the bash.rcfile
- Using the option –rcfile <filename>with bashallows you to execute commands from the specified file
- Excellent invocation features
- Can be invoked with the shcommand
- Bash can be run in a specific POSIX mode. Use set –o posixto invoke the mode or --posixon startup
- You can control the look of the prompt in Bash. Setting the PROMPT_COMMANDvariable to one or more of the special characterswill customize it for you
- Bash can also be invoked as a restricted shell(with rbashor --restricted), which means certain commands/actions are no longer allowed, such as:
- Setting or unsetting the values of the SHELL, PATH, ENV, or BASH_ENVvariables
- Redirecting output using the '>', '>|', '<>', '>&', '&>', and '>>' redirection operators
- Parsing the value of SHELLOPTS from the shell environment at startup
- Using the exec builtin to replace the shell with another command.
- And many more...
There are quite a few fans of the Z shell throughout the developer community who advocate for it quite heavily thanks to the many useful features it provides. On the other side, there are even more fans of Bash who know that their biggest advantage is their far larger user base. It's easy to see why it is so difficult to get developers to switch from Z shell to Bash and vice versa.
Hopefully now you have an idea as to how both Bash and Z shell differ, as well as their similarities. Quite a few resources exist out there for both shells, so you won't have any trouble finding more information if you need it.
Which shell do you prefer, and why? Let us know in the comments section!
下一篇：Ahead-Of-Time (AOT) Compilation May Come to OpenJDK HotSpot in Java 9