zsh-users
 help / color / mirror / code / Atom feed
From: Daniel Shahaf <d.s@daniel.shahaf.name>
To: zsh-users@zsh.org
Subject: Re: alias hygiene
Date: Sat, 17 Feb 2018 13:54:36 +0000	[thread overview]
Message-ID: <1518875676.3562728.1274073304.5ED24DC8@webmail.messagingengine.com> (raw)
In-Reply-To: <37bb7b6b-ad70-6d03-f979-0e0d3b0be4d3@eastlink.ca>

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.


  reply	other threads:[~2018-02-17 13:54 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
2018-02-17 13:54     ` Daniel Shahaf [this message]
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=1518875676.3562728.1274073304.5ED24DC8@webmail.messagingengine.com \
    --to=d.s@daniel.shahaf.name \
    --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).