Чем отличается bash от zsh
Перейти к содержимому

Чем отличается bash от zsh

  • автор:

Running a bash script from a zsh shell

I’m having problems running a bash script on zsh . The script is similar to the one below and is supposed to run under bash:

#!/bin/bash echo "" | egrep -oe [0-9]+ 

However, my shell is by default on zsh , so to run the above script what I do is type /bin/bash on my zsh prompt, and then I run . script.sh . However, this results in an error, and I get:

zsh: no matches found: [0-9]+ 
  1. If I am in bash , why do I get an error reported by zsh ?
  2. Why does the egrep regexp fail? The regular expression looks correct to me.

Note: In case it matters, I am not supposed to modify the script (the script is shared by multiple users and we are not supposed to make changes to it).

Amelio Vazquez-Reina
asked Sep 29, 2011 at 22:31
Amelio Vazquez-Reina Amelio Vazquez-Reina
41.3k 80 80 gold badges 202 202 silver badges 296 296 bronze badges

1 Answer 1

If the script properly begins with #!/bin/bash (you can’t have another comment before that), you should simply execute it by typing /path/to/script.sh , without that . at the beginning. The . is an include statement, it means “execute the contents of this file as if it had been typed here on the command line” (this is called “sourcing” in unix shell jargon).

Running bash and sourcing the script should work. I’m guessing that because you had bash automatically started in some circumstances, but you prefer zsh, you’ve set up bash to automatically switch over to zsh — maybe with exec zsh in your ~/.bashrc . Don’t do that: instead you should switch over to zsh in the file that’s executed when you log in, i.e. ~/.profile , and leave .bashrc alone. To have zsh be executed in terminals, set the SHELL environment variable:

# in ~/.profile export SHELL=/bin/zsh if [ -t 1 ]; then exec $SHELL; fi 

It’s unfortunate that you can’t modify the script, because it’s buggy. The brackets should be quoted, even in bash. If there is a file called 3+ in the current directory, the call to egrep will search for one or more occurrences of the digit 3 instead of a sequence of arbitrary digits. If there is a file called 3+ and a file called 4+ , the call to egrep will search for 3 s in the file 4+ . Here, the difference between bash and zsh only comes into play when no file matches the supplied pattern: bash silently runs the command with the unsubstituted pattern, whereas zsh signals an error (by default).

Bash vs Zsh: A comparison of two command line shells

Bash vs Zsh

If you have ever used the command line on a *nix system (Unix-based or Unix-like), you’ve probably used the bash shell. Bash (an abbreviation for “Bourne-again shell”) is the default shell for many Unix-like operating systems.

Common examples of Unix-like operating systems include GNU/Linux and macOS. Although many other shells exist for the terminal, one of the most popular shells is zsh or the “Z shell.”

While bash and zsh are powerful shells and share many prominent features, they also possess distinctions for which users may have different preferences. This post will overview the features and comparison of bash vs. zsh, how to set both as your default shell, and some configuration suggestions.

A short history of bash and zsh

In 1971, the very first version of the Unix operating system came with the Thompson shell as its default shell. While lacking in scripting capabilities, this first shell established many of the input/output redirection standards. With the release of Version 7 Unix in 1979, the new OS came distributed with the Bourne shell as its default shell.

Creation of bash

Later in 1989, Brian Fox created the Bash shell for the GNU Project as a viable software alternative for the Bourne shell. Bash introduced significant improvements over its predecessor. The most prominent of these improvements was its capacity as a scripting language, and bash users could write their programs for automating tasks.

These “shell” scripts were executable by typing the file name (typically with a .sh extension). For the most part, bash can support most of Bourne and C shell’s features and unique branches, including command-line editing and integer calculations.

Creation of zsh

Shortly after Bash emerged, a student at Princeton University created the Z shell or “zsh.” Hand in hand with this operating shell is a community-based online platform called Oh My Zsh, which features significant extensions such as plug-ins and themes for zsh. As of June 2019, macOS Catalina announced they would adopt zsh as their default shell to replace bash—a significant “win” for zsh enthusiasts.

wp engine hosting

Bash vs. zsh: Common features between bash and zsh

For the most part, bash vs. zsh share many convenient features that qualify both as highly efficient shells.

The z-command

One handy feature that both shares are the z command, which allows developers to keep track of their directories. With the z command, you can navigate to a frequently/recently visited directory by typing z, followed by the directory name. For example, say you have a directory newProject located at ~/src/2018/projects/newProject. Using this tool, you can type z newProject.

Auto-completion

Another practical aspect of both shells is their tab auto-complete functions. You can type the command within each shell, followed by -, then hit the tab. This action will then immediately display all available options for that command. You can then tab through the options until you find the one you want. Both shells offer flexibility and customization to their command-line auto-completion.

Auto-correction

Additionally, a helpful utility for a web design company in both shells, for the most part, is auto-correction. In the Z shell, if you make an innocent typo while writing a file location, spell correction is built-in and will automatically detect the typo. While this is not custom to bash, you can easily make the necessary adjustments to allow auto-correction by using shopt commands cdspell and dirspell.

Color customization

Even in terms of visual appeal and organization, both shells accomplish the task reasonably well. zsh will differentiate the two by adding color highlighting to directories. Additionally, the font weight will be slightly bolder. This is extremely helpful to see what’s a file and a directory. The exact colors used will depend on your terminal color settings. On the other hand, bash users can also tweak their interface to reflect their visual needs with the LS_colors variable.

While these are only some of the various tools offered by both shells, they provide a glimpse of how bash and zsh admittedly resemble each other. However, they both possess some distinctions that can create a preference for one over the other among developers.

bash-vs-zsh

Bash vs. zsh: Reasons to consider switching between shells

The commands and overall interaction for bash vs. zsh are essentially identical. Knowledge and familiarity with one shell can easily be translated into another without tackling a steep learning curve. However, both shells have unique features that may prompt developers to declare a clear preference.

For one, zsh has the leverage of a robust online community called Oh My Zsh . Oh, My ZSH is one of the oldest and most popular options for managing zsh configurations. Offering over 250 plugins and 140 different themes supplied by the community, Oh My ZSH is a great place to start customizing the z shell; that even comes with an auto-update function that keeps your shell updated. This allows users to work in a shell with a more personalized interface, amp up their workspace with various tools, and access a convenient out-of-the-box support system. For instance, a highly favorable option for the interface is a right-handed side prompt or a side prompt that auto-hides when typing in long file paths or commands. Even this minute level of flexibility turns developers’ heads toward zsh.

On the other hand, Bash has an impressive set of programming functions such as looping and conditional constructs, tilde and brace expansions, and the use of aliases. It also has its share of quirks, such as powerful invocation tools, being set into a restricted shell mode, having a particular POSIX mode, and more. Because Bash is also an older and established shell, there are unlimited online resources for this software.

bash-vs-zsh

Installing zsh

If you are on OS X, you already have zsh installed on your system. To check, run the following command:

If it is already installed, you should receive an output message that states /bin/zsh as the file path for the shell. There is an off-chance that it may display another file path if, for some reason, you moved the default installation directory. If you receive an output that states zsh not found, you must install it via Homebrew . You can do so by running the following command:

brew install zsh

For CentOS, Redhat, and Fedora Linux distributions, you can install zsh by running yum install zsh.

Similarly, sudo apt-get install zsh will install the shell on Debian and Ubuntu distributions. You can start using zsh even if bash is your default shell. To temporarily try it out, run zsh on the command line. This will temporarily switch the shell for your current session. To get the most out of zsh, upgrading your terminal is highly recommended instead of using the default.

The terminal is the default on Mac OS X. While it works perfectly fine, options such as iTerm 2> or Hyper offer enormous customization. I use iTerm 2, although Hyper is an excellent choice available cross-platform.

Setting zsh as your default shell

Now that you have zsh installed, all you have to do to set it as your default shell is run the following command:

To start using zsh, close your terminal window and open a new one — you will now be running zsh instead of bash.

Returning to bash

If, for some reason, you’d like to return to bash as your default shell, first find out where bash is installed with which bash.

The default path output should be /bin/bash.

To change back, use chsh -s /bin/bash.

Running bash will also temporarily switch the shell for that session.

Configuring zsh

Like bash’s .bashrc, zsh uses a dot file to store user configuration settings, .zshrc. Held in the home directory (~/.zshrc). There won’t be a configuration file setup by default, so you must create one in your home directory (~/.zshrc).

Similarly, you can add a .zprofile file to your home directory. .zprofile is a script that will run upon login. You can add any commands you’d like to run upon starting a new shell instance. Given the broad spectrum of customization available in zsh, using a dedicated configuration manager can be beneficial. Numerous options exist, including Prezto, > Oh My ZSH , and Antigen .

Oh My ZSH

To install Oh My ZSH, run

curl -L https://raw.github.com/robbyrussell/oh-my-zsh/master/tools/install.sh | sh

This will download the executable shell script and install it on your system. Upon installing Oh My ZSH, the default settings are a great place to start.

By default, it will install the ‘robbyrussell’ theme (its namesake coming from the project’s original developer).

Additionally, it will install several useful plugins. At least a few aspects of the terminal’s display will appear immediately different.

Git branches

Similar to the above, Oh My ZSH also adds a plugin that will display the current branch you are on in a git project. Appearing after the working directory in the command prompt, you’ll always know which branch you’re on. This way, you never have to git the branch to know the current one. The git plugin also adds several other valuable features.

In the example below, the project directory is currently on the tests branch. We can also know that the working tree is clean, with no changes you haven’t committed. In this case, this is indicated by the fact that the display is green. As soon as you make a change, the show turns another color, meaning that there are new changes to commit:

bash-vs-zsh

zsh significantly improves the Git workflow with these types of indications right in the command prompt.

Changing your theme

As mentioned above, zsh comes with hundreds of different themes. robbyrussell will be the default theme, although you can easily change this. To find a new theme offered by Oh My ZSH, preview each on the Themes page in the project repo. Given the many choices available, chances are you’ll find one available with almost precisely the features you’re looking for.

Once you find one you like, open up your .zshrc file in the home directory. Then, you’ll need to update the line starting with ZSH_THEME. Put the name of the name you want to use (inside of quotes), then save the file. Opening a new terminal session should now display the new theme.

bash-vs-zsh

You can even set random as your ZSH_THEME. This will load a new random theme each time you open up a new session in the shell.

Some themes, such as the popular Powerline fonts, may require additional packages or fonts. The theme’s installation instructions should specify any other packages needed, though.

Conclusion

As we’ve looked at in this zsh vs. bash post, zsh and bash share a similar and impressive array of features but also have their specific tools. If you’re looking for extensibility, greater customization, and advanced features not found in bash, the zsh shell combined with Oh My Zsh framework is an excellent choice. Regardless of what shell you choose, improved fluency on the command line provides countless benefits in efficiency, automation, and a better understanding of your computer’s inner workings.

For an introduction to some of the essential shell commands, check out our post on basic command line usage .

wp engine hosting

Author Bio

Angelo Frisina is a highly experienced author and digital marketing expert with over two decades of experience in the field. He specializes in web design, app development, SEO, and blockchain technologies.

Angelo’s extensive knowledge of these areas has led to the creation of several numerous award winning websites and mobile applications, as well as the implementation of effective digital marketing strategies for a wide range of clients.

Angelo is also a respected consultant, sharing his insights and expertise through various podcasts and online digital marketing resources.

With a passion for staying up-to-date with the latest trends and developments in the digital world, Angelo is a valuable asset to any organization looking to stay ahead in the digital landscape.

Zsh vs Bash

Zsh-vs-Bash

Interactive configuration features have many differences while comparing Zsh and Bash. Zsh is known for its extensibility, good customization, and advanced features. Since Zsh is made from Bash, almost 90% of the scripting is similar, and it is compatible with Bash. Both Zsh and Bash have many similarities and are easily portable. Many systems started using Zsh as their default shell.

Recommended Articles

This is a guide to Zsh vs Bash. Here we also discuss the Zsh vs Bash key differences with head to head comparison (infographics) and comparison table. You may also have a look at the following articles to learn more-

  1. Rust vs C++
  2. Physical Address vs Logical Address
  3. List vs Set – Useful Comparisons
  4. Array vs ArrayList
  5. What is Bash? | Concept of Bash | History

ADVERTISEMENT
All-in-One Excel VBA Bundle — 120+ Courses | 110+ Mock Tests | 500+ Hours | Lifetime |
120+ Online Courses
30+ Projects
500+ Hours
Verifiable Certificates
Lifetime Access
ADVERTISEMENT
Financial Analyst Masters Training Program

2000+ Hours of HD Videos
43 Learning Paths
550+ Courses
Verifiable Certificate of Completion
Lifetime Access

ADVERTISEMENT
All in One Data Science Bundle

2000+ Hour of HD Videos
80 Learning Paths
400+ Courses
Verifiable Certificate of Completion
Lifetime Access

ADVERTISEMENT
All in One Software Development Bundle

5000+ Hours of HD Videos
149 Learning Paths
1050+ Courses
Verifiable Certificate of Completion
Lifetime Access

Primary Sidebar

All in One Software Development Bundle 5000+ Hours of HD Videos | 149 Learning Paths | 1050+ Courses | Verifiable Certificate of Completion | Lifetime Access

Financial Analyst Masters Training Program 2000+ Hours of HD Videos | 43 Learning Paths | 550+ Courses | Verifiable Certificate of Completion | Lifetime Access

Добавить комментарий

Ваш адрес email не будет опубликован. Обязательные поля помечены *