zsh-users
 help / color / mirror / code / Atom feed
* egrep with zsh as /bin/sh - wordsplitting
@ 2005-01-13 20:50 Michael Prokop
  2005-01-13 21:23 ` Danek Duvall
  2005-01-14  8:43 ` Stephane Chazelas
  0 siblings, 2 replies; 4+ messages in thread
From: Michael Prokop @ 2005-01-13 20:50 UTC (permalink / raw)
  To: Zsh users list

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/


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: egrep with zsh as /bin/sh - wordsplitting
  2005-01-13 20:50 egrep with zsh as /bin/sh - wordsplitting Michael Prokop
@ 2005-01-13 21:23 ` Danek Duvall
  2005-01-14  8:43 ` Stephane Chazelas
  1 sibling, 0 replies; 4+ messages in thread
From: Danek Duvall @ 2005-01-13 21:23 UTC (permalink / raw)
  To: Zsh users list

On Thu, Jan 13, 2005 at 09:50:18PM +0100, Michael Prokop wrote:

> Why does the original egrep version use ${1+"$@"} and not
> "$@" or "${1+"$@"}"?

My understanding is that if you write "$@", then with no arguments, in some
shells it becomes "" (i.e., an argument, but with no length), and that's
not actually what you want.  ${1+"$@"} only adds something if there are any
arguments.

I don't know if this is a problem on any modern shell, though.

Danek


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: egrep with zsh as /bin/sh - wordsplitting
  2005-01-13 20:50 egrep with zsh as /bin/sh - wordsplitting Michael Prokop
  2005-01-13 21:23 ` Danek Duvall
@ 2005-01-14  8:43 ` Stephane Chazelas
  2005-01-14 17:15   ` Michael Prokop
  1 sibling, 1 reply; 4+ messages in thread
From: Stephane Chazelas @ 2005-01-14  8:43 UTC (permalink / raw)
  To: Zsh users list

On Thu, Jan 13, 2005 at 09:50:18PM +0100, Michael Prokop wrote:
> 
> 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+"$@"}
> `----
[...]

That's a known issue, and the generally provided work-around is:

test -n "${ZSH_VERSION+set}" && alias -g '${1+"$@"}="$@"'

See info -f autoconf -n 'Shell Substitutions'

That should be

#!/bin/sh -

BTW.

-- 
Stéphane


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: egrep with zsh as /bin/sh - wordsplitting
  2005-01-14  8:43 ` Stephane Chazelas
@ 2005-01-14 17:15   ` Michael Prokop
  0 siblings, 0 replies; 4+ messages in thread
From: Michael Prokop @ 2005-01-14 17:15 UTC (permalink / raw)
  To: Zsh users list

* Stephane Chazelas <Stephane_Chazelas@yahoo.fr> [20050114 09:55]:
> On Thu, Jan 13, 2005 at 09:50:18PM +0100, Michael Prokop wrote:

> > 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+"$@"}
> > `----
> [...]

> That's a known issue, and the generally provided work-around is:

> test -n "${ZSH_VERSION+set}" && alias -g '${1+"$@"}="$@"'

Should I just add this line to /etc/zsh/zshenv?
Because I'd like to fix this issue not just for egrep/fgrep
but in general. ;-)

BTW: Are there any other issues on using zsh as /bin/sh I should
know of?

> See info -f autoconf -n 'Shell Substitutions'

Thanks for the pointer!

> That should be

> #!/bin/sh -

> BTW.

Could you please provide me more information regarding this?

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


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2005-01-14 17:16 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2005-01-13 20:50 egrep with zsh as /bin/sh - wordsplitting Michael Prokop
2005-01-13 21:23 ` Danek Duvall
2005-01-14  8:43 ` Stephane Chazelas
2005-01-14 17:15   ` Michael Prokop

Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).