zsh-users
 help / color / mirror / code / Atom feed
From: Ray Andrews <rayandrews@eastlink.ca>
To: zsh-users@zsh.org
Subject: Re: alias hygiene
Date: Mon, 19 Feb 2018 10:14:45 -0800	[thread overview]
Message-ID: <ae0d08e1-393d-f683-9538-299f4f06bea2@eastlink.ca> (raw)
In-Reply-To: <180218222847.ZM11922@torch.brasslantern.com>

On 18/02/18 10:28 PM, Bart Schaefer wrote:
> To add a few remarks here ...
There's meat here even though the bone itself was not really cooked, I 
was just interested in the elementary question of a conditional alias, 
the rest of it was so raw as to not even reflect my own level of knowledge.

> If we look at the first
> line only, the quotes are such that $string must have a value at the
> time the alias is defined (rather than when it is used)
Right, I do understand that, the whole point of the alias was to capture 
some values at the time of definition and others at the time of 
execution.  I've since moved on to a function and it's much more 
robust.  As I said, I was just chicken about passing: "^|$1|^$string"   
... to a function, I was sure it would ruin my life as the parser tried 
to eat it.   I've gotten over that.  Big boys don't cry.
>   and there is a
> useless empty quote pair at the end of the line.

Useless he says!  Sheesh, half my problems are not having enough quotes, 
so I tend to try to solve every problem by throwing more quotes at 
things ;-)  *then*, once it works, I see how many I can pull out.
> Although it may seem
> you can nest double quotes when using $(...) or some ${...} syntax --
> 	echo "Outer and $(echo "inner also") quotes"
> -- the presence of the $(...) is important.  In the absence of any of
> the balanced-parens or balanced-braces inside, quotes do not nest.
You can see how that's logical,  $() must have quotes within as 
unaffected by quotes without.  I can't explain it, but I know it's 
true.  Still it's the sort of thing one tattoos on one's arm: quotes do 
not nest.
>
> } 1. Calling the alias "_grep" clashes with compsys's function of that name.
>
> This isn't really important; the completion system disables aliases, and
> it would not make sense to use the _grep function outside completion, so
> in practice there's no conflict.
It was actually   "  _ggrep   "  so no issue anyway.   I like to 
deliberately misspell any identifier that might be confused with a 
normal word or some other standard identifier.

>
>      alias _grep='() { egrep --color=always "^|$1|$string" }'
That is a revelation, we can have our cake and eat it too!
>
>
>      () { egrep --color=always "^|$1|$string" } foo
>
> which would pass "foo" to the anonymous sub as "$1".
Right, it's even intuitive.  Bloody marvellous.
>
>
> } 4. Those pipe characters are not quoted so they create a pipeline
>
> Another likely case of incorrect quote nesting.
Yeah, I got all that sorted out.  Sorry again for the half-cooked snip.
>
> } More information:
> }
> } 0. "||" is invalid at the start of a logical line.
>
> Most likely line wrap added in a syntactically invalid place.  (Aside:
> The discussion that led up to the ability to make aliases for command
> separators was spawned by a desire to be able to put "||" at the start
> of a logical line.)
I remember querying that some time ago.  IIRC there were subtly 
different rules for " && " and " || " anyway no matter, line 
continuation solves all issues.
> One other remark about aliases with anonymous functions:  It's dangerous
> to do this with global aliases.  Example:
>
>      alias -g BODY='() { echo I am a function body }'
>      echo this is a BODY
>
> will (with default setopts) create four functions (echo, this, is, a)
> all with the same definition -- and that "echo" function calls itself.
>
Holy Cow.  Best to avoid the deeper magic.



      reply	other threads:[~2018-02-19 18:14 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
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 [this message]

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=ae0d08e1-393d-f683-9538-299f4f06bea2@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).