zsh-users
 help / color / mirror / code / Atom feed
From: Ray Andrews <rayandrews@eastlink.ca>
To: zsh-users@zsh.org
Subject: Re: alias hygiene
Date: Fri, 16 Feb 2018 19:14:42 -0800	[thread overview]
Message-ID: <37bb7b6b-ad70-6d03-f979-0e0d3b0be4d3@eastlink.ca> (raw)
In-Reply-To: <1518827410.2159479.1273706656.1F5B436D@webmail.messagingengine.com>

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


  reply	other threads:[~2018-02-17  3:44 UTC|newest]

Thread overview: 19+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-02-14 16:09 Ray Andrews
2018-02-17  0:30 ` Daniel Shahaf
2018-02-17  3:14   ` Ray Andrews [this message]
2018-02-17 13:54     ` Daniel Shahaf
2018-02-17 15:20       ` Ray Andrews
2018-02-17 15:30         ` Daniel Shahaf
2018-02-17 15:42           ` Ray Andrews
2018-02-17 22:04             ` Bart Schaefer
2018-02-18  0:17               ` Ray Andrews
2018-02-18  2:10                 ` Bart Schaefer
2018-02-18  3:38                   ` Ray Andrews
2018-02-19  6:37                     ` Bart Schaefer
2018-02-27 13:48           ` Vincent Lefevre
2018-02-27 15:26             ` Ray Andrews
2018-02-17 15:36       ` Daniel Shahaf
2018-02-17 21:40     ` Bart Schaefer
2018-02-18  0:08       ` Ray Andrews
2018-02-19  6:28   ` Bart Schaefer
2018-02-19 18:14     ` Ray Andrews

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=37bb7b6b-ad70-6d03-f979-0e0d3b0be4d3@eastlink.ca \
    --to=rayandrews@eastlink.ca \
    --cc=zsh-users@zsh.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).