Re: zsh preexec() positional parameters

From: Stachu 'Dozzie' K. (dozzie_at_dynamit.im.pwr.wroc.pl.nospam)
Date: 11/20/04


Date: Sat, 20 Nov 2004 00:53:26 +0000 (UTC)

On 2004-11-19, N. Dehne wrote:
> Hi,
>
> after playing around with preexec()[1] I noticed some oddities.
>
> First, I made the mistake of using $@, thinking that everything typed as
> a command would be split up. With this I noticed that I got the executed
> command three times in $@.
>
> So I used just $1 and all was well. Then I thought, what are $2 and $3
> for actually? I noticed that $2 and $3 are expanding aliases for example.
>
> Now, something else bit me:
>
> Typing a very long command affects $2 but not $1 and $3, e.g.:
>
> preexec () {
> echo "preexec: $1"
> echo "preexec: $2"
> echo "preexec: $3"
> }
>
> $ echo
> ddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd
>
> preexec: echo
> ddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd
> preexec: echo
> dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd
> preexec: echo
> ddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd
> ddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd
>
> Notice how $2 is truncated by 1 char. If I make the command even longer
> $2 ends up being just echo without the argument.
>
> Now, what is the exact terminology that $1 $2 and $3 in preexec()
> represent? Ideally, which should I use to output the current command to
> a hardstatus line (xterm title)?

info zsh -> Functions
or
man zshmisc -> /preexec

#v+
       preexec
              Executed just after a command has been read and is
              about to be executed. If the history mechanism is
              active (and the line was not discarded from the
              history buffer), the string that the user typed is
              passed as the first argument, otherwise it is an
              empty string. The actual command that will be exe­
              cuted (including expanded aliases) is passed in two
              different forms: the second argument is a sin­
              gle-line, size-limited version of the command (with
              things like function bodies elided); the third
              argument contains the full text what what is being
              executed.
#v-

Difference between $2 and $3 you've just seen. Difference between $1 and
$2 is visible when you execute some alias or function.

-- 
Stanislaw Klekot