Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

egrep with zsh as /bin/sh - wordsplitting



Hello,

I'm maintainer of a Linux Live-CD with zsh as it's default shell.
/bin/sh is pointing to /bin/zsh.

I've a problem with the scripts egrep and fgrep.
That's the original egrep-script:

,---- [ /bin/egrep - original version ]
| #!/bin/sh
| exec grep -E ${1+"$@"}
`----

egrep in scripts (for example in 'translate') which use
'#!/bin/sh' as the shebang-line results in problems due to
wordsplitting.

I stumbled upon:
http://lists.gnu.org/archive/html/bug-gnu-utils/2002-04/msg00361.html
http://www.zsh.org/mla/workers//2002/msg00546.html

The solution would be to adjust egrep (and fgrep):

,---- [ /bin/egrep for zsh, not portable ]
| #!/bin/sh
| setopt noshwordsplit
| exec grep -E ${1+"$@"}
`----

or:

,---- [ /bin/egrep for zsh, portable? ]
| #!/bin/sh
| exec grep -E "$@"
`----

or:

,---- [ /bin/egrep for zsh, probably more portable? ]
| #!/bin/sh
| if test -n "${ZSH_VERSION+set}"; then
|   exec grep -E "$@"
| else
|   exec grep -E ${1+"$@"}
| fi
`----

Refering to Peter [1] the following works also in bash, but not with
sh on SunOS 5.8:

,---- [ /bin/egrep for zsh - with quotes]
| #!/bin/sh
| exec grep -E "${1+"$@"}"
`----

Why does the original egrep version use ${1+"$@"} and not
"$@" or "${1+"$@"}"? Which ones are shell-compatible?
Refering to [1] the ${1+"$@"}-version handles zero arguments
correctly. Which version should be prefered in general
(to be sh-compatible)?

Which of the above versions would you suggest to take for
egrep/fgrep? The last ("${1+"$@"}") one?

[1] http://www.zsh.org/mla/workers//2002/msg00544.html

thanks for any feedback && regards,
-mika-
-- 
 ,'"`.         http://www.michael-prokop.at/
(  grml.org -» Linux for texttool-users and sysadmins
 `._,          http://www.grml.org/



Messages sorted by: Reverse Date, Date, Thread, Author