Zsh Mailing List Archive
Messages sorted by:
Reverse Date,
Date,
Thread,
Author
Re: alias hygiene
- X-seq: zsh-users 23132
- From: Daniel Shahaf <d.s@xxxxxxxxxxxxxxxxxx>
- To: zsh-users@xxxxxxx
- Subject: Re: alias hygiene
- Date: Sat, 17 Feb 2018 00:30:10 +0000
- Dkim-signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= daniel.shahaf.name; h=content-transfer-encoding:content-type :date:from:in-reply-to:message-id:mime-version:references :subject:to:x-me-sender:x-me-sender:x-sasl-enc; s=fm2; bh=Cj6+7o mF2/CcXNTw6C4Tlzk+5PtPoaA+gAoUcTD/dko=; b=OgvFI/2S/Z7CNhR5NHikg8 kB672fo50rKnjgzfb37E72CV9ysi6GmdsUf110iEkdMQLUirIztqQIIW0R16Po3Q guLFHQ9Zslz5SM+itETT1bvnAJmrlbNfmzCriRl8vvvKhBP57no9dX9DO5m8AgZ1 onau3eO0NjnSUoT8SxTtNYRY5ze7H14hBSo4qFY8seZmYFZBpyA5YnEpubYNbB+2 eE5GdBvyJ1wCOrTZnvr8pDNfHOnzlCUW28HrI6obWuMt/xzSyNB5R03EKRdGtyDc hGT/aC2mQRYvIKAabMO0GxT2qsGP9Tph2MVzatQ/DfwNu7YLymQQNnbDouw8xSUw ==
- Dkim-signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=content-transfer-encoding:content-type :date:from:in-reply-to:message-id:mime-version:references :subject:to:x-me-sender:x-me-sender:x-sasl-enc; s=fm2; bh=Cj6+7o mF2/CcXNTw6C4Tlzk+5PtPoaA+gAoUcTD/dko=; b=drfYKSmEMVhiT3ypxBAeDP NRjov/T4r2A/3QsXpyRNgN1adzHRU5ioT9Mc42WCrhenqBRh55KKlWz7POY87H9y WzIsJc6WrkwbKEdolEyaoflM9m4S5UkYqb2EMZDpAHDsNcPd+9MOY2H9KJkUk4sf Te49UJtvs1dTagAzEg4AwtE7yREXgQDsndPMkFJljOwc00eG4A/Hqv8utLn1EpJZ HhwpGI9C9HqztJooQW8pKaUTgMks7zIiUY34LXmOJvS5yFu4ds/TgDuHwPBxGnF8 giLLPpOXxS8qcKDM6VXhNQ9tNiS1Wy0ndattma+xILp5w0+zBBLfgox4s7dTapmQ ==
- In-reply-to: <1679a1e0-f716-e501-c4c2-f7e485f9701f@eastlink.ca>
- List-help: <mailto:zsh-users-help@zsh.org>
- List-id: Zsh Users List <zsh-users.zsh.org>
- List-post: <mailto:zsh-users@zsh.org>
- List-unsubscribe: <mailto:zsh-users-unsubscribe@zsh.org>
- Mailing-list: contact zsh-users-help@xxxxxxx; run by ezmlm
- References: <1679a1e0-f716-e501-c4c2-f7e485f9701f@eastlink.ca>
Ray Andrews wrote on Wed, 14 Feb 2018 08:09 -0800:
>
> [ some test ] && alias _grep="egrep --color=always "$string""
> || alias _grep="egrep --color=always "^|$1|$string""
>
> ... the alias will end up downstream from a pipe which is why there's no
> filespec.
>
> Is that sort of thing sanitary?
I don't understand your question, but allow me to review this code _without_
knowing its context or purpose:
0. It's a syntax error.
1. Calling the alias «_grep» clashes with compsys's function of that name.
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).
3. Do not interpolate strings into command strings; that's a bobby tables bug.
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.
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.
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).
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