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

Re: alias hygiene



Ray Andrews wrote on Wed, 14 Feb 2018 08:09 -0800:
> 
>      [ some test ] && alias _grep="egrep --color=always  "$string""
>                              || alias _grep="egrep --color=always "^|$1|$string""
> 
> ... the alias will end up downstream from a pipe which is why there's no 
> filespec.
> 
> Is that sort of thing sanitary?

I don't understand your question, but allow me to review this code _without_
knowing its context or purpose:

0. It's a syntax error.

1. Calling the alias «_grep» clashes with compsys's function of that name.

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

3. Do not interpolate strings into command strings; that's a bobby tables bug.

4. Those pipe characters are not quoted so they create a pipeline, which you
did not intend.  It would appear that you have not tested that codepath.

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.

1. That matters because aliases and functions are both invoked by entering their
name in command position.  It's possible this doesn't have any effect (depends
on how you autoload compinit) but it's still a bug.

2. «$1» would be fine in specific circumstances, but it's usually a mistake.  If
that code is in zshrc toplevel then the use of «$1» is definitely a mistake.

3. «alias foo='egrep --color=always -- ${(q)string}'» would be better (although
I do not vouch that that --color=always switch is correct in the wider context).
In other contexts arrays can be used (but not here).

4. The command string «print -lr "foo "bar baz" qux"» has four words: «print», «-lr»,
«foo bar», and «baz qux».



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