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

Re: alias hygiene



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.




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