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

Re: alias hygiene



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.

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.

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.   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 so I thought to just leave it there for the expanded alias to eat, that is, for egrep to eat.  So I guess I'm being a bit chicken not using a function.

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.

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.

I suspect the context would show that all right, I do end up with my pipes working ok.  It's rather convoluted, sorry if the snip was not satisfactory, I was essentially worried about the conditional alias being a good idea from the getgo, not about the subsequent details which are not complete.  I don't think I've ever seen a conditional alias before.


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.

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

Thanks, I forget about ' (q) '.

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