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

Re: alias hygiene



Ray Andrews wrote on Fri, 16 Feb 2018 19:14 -0800:
> On 16/02/18 04:30 PM, Daniel Shahaf wrote:
> >
> > I don't understand your question, but allow me to review this code _without_
> > knowing its context or purpose:
> Thanks Daniel, I did suspect it would not be kosher.  Is there some way 
> to see how this looks to the parser after expansion?  That way I'd 
> probably be able to understand the issue better.

You could use a widget such as:

[[[
f() {
  local arg
  for arg in ${(z)1}; do
    print -r -l -- «${(Q)arg}»
  done
}
show-buffer-parse() {
  zle -M "$(f "$PREBUFFER$LBUFFER")"
}
zle -N show-buffer-parse
bindkey '^T' show-buffer-parse
]]]

There may be plugins that could help, too.

> >
> > 0. It's a syntax error.
> >
> > 1. Calling the alias «_grep» clashes with compsys's function of that name.
> Ah!  So I could  just rename it as far as that goes?  It does seem to 
> work fine, but this sounds like a gotcha.

Yes, just rename it.

> > 2. Aliases don't have positional parameters.  That «$1» is syntactically valid
> > but I am not sure whether it does what you want (or, rather, whether it would
> > do what you want after you fix #4).
> Actually that's the reason for the alias, I want it to pick up the 
> parameters inline after the alias is expanded.

That does not require using $1:

    % alias x='echo foo'
    % x qux

However, note also:

    % alias x='echo foo; echo bar'
    % x qux

If you wanted that to print «foo qux», you would have had to use a function.

The use of $1 in your excerpt is a latent bug.

> It does what I want but 
> I question its safety.  In practical terms the 
> "^|$string|$second_string"   construction is not something I want to try 
> passing to a function

You really, really have to learn quoting and escaping.  There is no problem at
all with strings that contain variable substitutions and shell and extendedglob
metacharacters, provided that they are quoted.

I refer you again to the second point #4 in my previous email.  Without running
that command, what do you expect it to output?  Now run it.  Do you understand
why it behaved as it did?

> so I thought to just leave it there for the 
> expanded alias to eat, that is, for egrep to eat.

That pipe is not quoted, hence it is syntactical.  Alias definitions are simple
command so they end at a command separator — which includes «;» and «&&» and
«|», among others.

> >
> > 3. Do not interpolate strings into command strings; that's a bobby tables bug.
> This is what I had expected the issue would be.  Mind, I cobble strings 
> together into command strings all the time and I've gotten used to the 
> little tweaks needed.  How can I read up on this 'bobby tables'?  I 
> might have developed some bad habits.

You might do a web search for that term.

> > More information:
> >
> > 0. «||» is invalid at the start of a logical line.  Either there is an error
> > message you did not tell us about or there is a difference between what you ran
> > and what you posted.
> 
> Right, context again, I should only show what is strictly relevant to 
> the question.

I was trying to point out that your report was either incomplete or inaccurate,
not that it had excessive detail.



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