Take the 2-minute tour ×

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:

echo "<235234>"  | 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]+

(I should get 235234 instead).

At this point I am confused and have the following questions:

  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).


share|improve this question
add comment

1 Answer

up vote 11 down vote accepted

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 3s 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).

share|improve this answer
Thanks @Guilles. This was very helpful. The odd thing is that I don't have exec zsh on my ~/.bashrc, nor on my ~/.profile (I don't even have a .profile on my home directory). I don't recall how I managed to make zsh my default shell. Also, sorry for the confusion, but the first line on my script is the shebang #!/bin/bash. With this, I still don't know why sourcing the file from zsh doesn't work (if you have any thoughts about this let me know). In either case, I'll contact the administrator to update the script though to add quotes to the regexp. –  user815423426 Sep 30 '11 at 1:14
The problem with sourcing it in zsh is the second argument to egrep. –  Ignacio Vazquez-Abrams Sep 30 '11 at 2:21
add comment

Your Answer


