* Alias call in function fails... @ 2020-06-22 12:49 Frank Gallacher 2020-06-22 23:23 ` Mikael Magnusson 0 siblings, 1 reply; 24+ messages in thread From: Frank Gallacher @ 2020-06-22 12:49 UTC (permalink / raw) To: zsh-users [-- Attachment #1.1: Type: text/plain, Size: 1365 bytes --] Greetings, I have a function: dumpall () { if [ $# -eq 0 ] then echo "dumpall: Usage 'dumpall {files}'" else for FILENAME in $* do if [ ! -e ${FILENAME} ] then echo "dumpall: ${FILENAME} does not exist" else echo -e "\033[31m:::::::: ${FILENAME} ::::::::\033[0m" dumpit ${FILENAME} fi done fi } Which calls an alias: alias dumpit='hexdump -C -n 128' It works fine in bash, but with zsh now I get: :::::::: test.txt :::::::: dumpall:11: command not found: dumpit Am I doing something wrong??? TIA, will summarise u Frank C.Gallacher phone. (03) 9429 9234 int. +61 3 9429 9234 mob. +61 419 893 306 |\/\/\/| | | | (o)(o) C _) | ,___| | / My favourite palindrome is: “Satan oscillate my metallic sonatas <<< MSG ENDS >>> [-- Attachment #1.2: Type: text/html, Size: 12910 bytes --] [-- Attachment #2: image001.png --] [-- Type: image/png, Size: 904 bytes --] ^ permalink raw reply [flat|nested] 24+ messages in thread
* Re: Alias call in function fails... 2020-06-22 12:49 Alias call in function fails Frank Gallacher @ 2020-06-22 23:23 ` Mikael Magnusson 2020-06-23 8:37 ` Andreas Kusalananda Kähäri 2020-06-29 16:24 ` Sebastian Gniazdowski 0 siblings, 2 replies; 24+ messages in thread From: Mikael Magnusson @ 2020-06-22 23:23 UTC (permalink / raw) To: Frank Gallacher; +Cc: zsh-users On 6/22/20, Frank Gallacher <franxg@gmail.com> wrote: > Greetings, > > I have a function: > [...] > > Which calls an alias: > > alias dumpit='hexdump -C -n 128' > > It works fine in bash, but with zsh now I get: > > :::::::: test.txt :::::::: > dumpall:11: command not found: dumpit > > Am I doing something wrong??? Yes, don't use aliases for anything but interactive usage. Eg if you want to use dumpit in a script, make it be a function: dumpit() { hexdump -C -n 128 } Aliases are expanded on parse time, which means aliases defined in a file won't be usable in that same file. (Because it is parsed in its entirety before any of the code is actually run). -- Mikael Magnusson ^ permalink raw reply [flat|nested] 24+ messages in thread
* Re: Alias call in function fails... 2020-06-22 23:23 ` Mikael Magnusson @ 2020-06-23 8:37 ` Andreas Kusalananda Kähäri 2020-06-23 9:14 ` Peter Stephenson 2020-06-23 9:55 ` Mikael Magnusson 2020-06-29 16:24 ` Sebastian Gniazdowski 1 sibling, 2 replies; 24+ messages in thread From: Andreas Kusalananda Kähäri @ 2020-06-23 8:37 UTC (permalink / raw) To: Mikael Magnusson; +Cc: Frank Gallacher, zsh-users On Tue, Jun 23, 2020 at 01:23:55AM +0200, Mikael Magnusson wrote: > On 6/22/20, Frank Gallacher <franxg@gmail.com> wrote: > > Greetings, > > > > I have a function: > > > [...] > > > > Which calls an alias: > > > > alias dumpit='hexdump -C -n 128' > > > > It works fine in bash, but with zsh now I get: > > > > :::::::: test.txt :::::::: > > dumpall:11: command not found: dumpit > > > > Am I doing something wrong??? > > Yes, don't use aliases for anything but interactive usage. Eg if you > want to use dumpit in a script, make it be a function: > dumpit() { hexdump -C -n 128 } > > Aliases are expanded on parse time, which means aliases defined in a > file won't be usable in that same file. (Because it is parsed in its > entirety before any of the code is actually run). > > -- > Mikael Magnusson If the alias is defined before the definition of the function, it ought to work. -- Andreas (Kusalananda) Kähäri SciLifeLab, NBIS, ICM Uppsala University, Sweden . ^ permalink raw reply [flat|nested] 24+ messages in thread
* Re: Alias call in function fails... 2020-06-23 8:37 ` Andreas Kusalananda Kähäri @ 2020-06-23 9:14 ` Peter Stephenson 2020-06-23 9:55 ` Mikael Magnusson 1 sibling, 0 replies; 24+ messages in thread From: Peter Stephenson @ 2020-06-23 9:14 UTC (permalink / raw) To: zsh-users > On 23 June 2020 at 09:37 Andreas Kusalananda Kähäri <andreas.kahari@abc.se> wrote: > On Tue, Jun 23, 2020 at 01:23:55AM +0200, Mikael Magnusson wrote: >> On 6/22/20, Frank Gallacher <franxg@gmail.com> wrote: >>> Greetings, >>> >>> I have a function: >>> >> [...] >>> >>> Which calls an alias: >>> >>> alias dumpit='hexdump -C -n 128' >>> >>> It works fine in bash, but with zsh now I get: >>> >>> :::::::: test.txt :::::::: >>> dumpall:11: command not found: dumpit >>> >>> Am I doing something wrong??? >> >> Aliases are expanded on parse time, which means aliases defined in a >> file won't be usable in that same file. (Because it is parsed in its >> entirety before any of the code is actually run). > > If the alias is defined before the definition of the function, it ought to work. It's a bit more complicated than that. There's a difference between a file that's being executed line by line, which includes initialisation files and sourced files as well as scripts, and a file that's parsed and then executed, which would include an autoloaded function definition. In the former case, the alias is defined before the function definition is executed; in the latter case it isn't, even though the line with the alias definitions appears before the function definition. For sure you can't define the alias *after* the function definition. pws ^ permalink raw reply [flat|nested] 24+ messages in thread
* Re: Alias call in function fails... 2020-06-23 8:37 ` Andreas Kusalananda Kähäri 2020-06-23 9:14 ` Peter Stephenson @ 2020-06-23 9:55 ` Mikael Magnusson 2020-06-23 11:28 ` Andreas Kusalananda Kähäri 1 sibling, 1 reply; 24+ messages in thread From: Mikael Magnusson @ 2020-06-23 9:55 UTC (permalink / raw) To: Mikael Magnusson, Frank Gallacher, zsh-users On 6/23/20, Andreas Kusalananda Kähäri <andreas.kahari@abc.se> wrote: > On Tue, Jun 23, 2020 at 01:23:55AM +0200, Mikael Magnusson wrote: >> Aliases are expanded on parse time, which means aliases defined in a >> file won't be usable in that same file. (Because it is parsed in its >> entirety before any of the code is actually run). > > If the alias is defined before the definition of the function, it ought to > work. As I already explained, it will not. -- Mikael Magnusson ^ permalink raw reply [flat|nested] 24+ messages in thread
* Re: Alias call in function fails... 2020-06-23 9:55 ` Mikael Magnusson @ 2020-06-23 11:28 ` Andreas Kusalananda Kähäri 2020-06-23 12:04 ` Daniel Shahaf 0 siblings, 1 reply; 24+ messages in thread From: Andreas Kusalananda Kähäri @ 2020-06-23 11:28 UTC (permalink / raw) To: Mikael Magnusson; +Cc: Frank Gallacher, zsh-users On Tue, Jun 23, 2020 at 11:55:20AM +0200, Mikael Magnusson wrote: > On 6/23/20, Andreas Kusalananda Kähäri <andreas.kahari@abc.se> wrote: > > On Tue, Jun 23, 2020 at 01:23:55AM +0200, Mikael Magnusson wrote: > >> Aliases are expanded on parse time, which means aliases defined in a > >> file won't be usable in that same file. (Because it is parsed in its > >> entirety before any of the code is actually run). > > > > If the alias is defined before the definition of the function, it ought to > > work. > > As I already explained, it will not. > > -- > Mikael Magnusson As Peter Stephenson explained, it depends. -- Andreas (Kusalananda) Kähäri SciLifeLab, NBIS, ICM Uppsala University, Sweden . ^ permalink raw reply [flat|nested] 24+ messages in thread
* Re: Alias call in function fails... 2020-06-23 11:28 ` Andreas Kusalananda Kähäri @ 2020-06-23 12:04 ` Daniel Shahaf 2020-06-23 12:46 ` Perry Smith 0 siblings, 1 reply; 24+ messages in thread From: Daniel Shahaf @ 2020-06-23 12:04 UTC (permalink / raw) To: zsh-users Andreas Kusalananda Kähäri wrote on Tue, 23 Jun 2020 13:28 +0200: > On Tue, Jun 23, 2020 at 11:55:20AM +0200, Mikael Magnusson wrote: > > On 6/23/20, Andreas Kusalananda Kähäri <andreas.kahari@abc.se> wrote: > > > On Tue, Jun 23, 2020 at 01:23:55AM +0200, Mikael Magnusson wrote: > > >> Aliases are expanded on parse time, which means aliases defined in a > > >> file won't be usable in that same file. (Because it is parsed in its > > >> entirety before any of the code is actually run). > > > > > > If the alias is defined before the definition of the function, it ought to > > > work. > > > > As I already explained, it will not. > > > > As Peter Stephenson explained, it depends. > See also the ALIAS_FUNC_DEF option. Cheers, Daniel ^ permalink raw reply [flat|nested] 24+ messages in thread
* Re: Alias call in function fails... 2020-06-23 12:04 ` Daniel Shahaf @ 2020-06-23 12:46 ` Perry Smith 2020-06-23 13:10 ` Kamil Dudka 0 siblings, 1 reply; 24+ messages in thread From: Perry Smith @ 2020-06-23 12:46 UTC (permalink / raw) To: Daniel Shahaf; +Cc: zsh-users > On Jun 23, 2020, at 7:04 AM, Daniel Shahaf <d.s@daniel.shahaf.name> wrote: > > Andreas Kusalananda Kähäri wrote on Tue, 23 Jun 2020 13:28 +0200: >> On Tue, Jun 23, 2020 at 11:55:20AM +0200, Mikael Magnusson wrote: >>> On 6/23/20, Andreas Kusalananda Kähäri <andreas.kahari@abc.se> wrote: >>>> On Tue, Jun 23, 2020 at 01:23:55AM +0200, Mikael Magnusson wrote: >>>>> Aliases are expanded on parse time, which means aliases defined in a >>>>> file won't be usable in that same file. (Because it is parsed in its >>>>> entirety before any of the code is actually run). >>>> >>>> If the alias is defined before the definition of the function, it ought to >>>> work. >>> >>> As I already explained, it will not. >>> >> >> As Peter Stephenson explained, it depends. >> > > See also the ALIAS_FUNC_DEF option. My amateur 2 cents … I gave up on aliases with bash and never tried them with zsh. Aliases are just weird oddities that have lots of special cases. Since functions now exist (aliases came first in the global history of shells), I don’t see any advantages to aliases and they have many problems. I don’t know of anything an alias can do that a function can’t? ^ permalink raw reply [flat|nested] 24+ messages in thread
* Re: Alias call in function fails... 2020-06-23 12:46 ` Perry Smith @ 2020-06-23 13:10 ` Kamil Dudka 2020-06-23 16:03 ` Bart Schaefer 0 siblings, 1 reply; 24+ messages in thread From: Kamil Dudka @ 2020-06-23 13:10 UTC (permalink / raw) To: Perry Smith; +Cc: zsh-users On Tuesday, June 23, 2020 2:46:50 PM CEST Perry Smith wrote: > My amateur 2 cents … I gave up on aliases with bash and never tried them > with zsh. > > Aliases are just weird oddities that have lots of special cases. Since > functions now exist (aliases came first in the global history of shells), I > don’t see any advantages to aliases and they have many problems. > > I don’t know of anything an alias can do that a function can’t? Yet such things exist. You can find many use cases for them in the User’s Guide to the Z-Shell, as for example: % alias %=' ' % alias mkdir='nocorrect mkdir' % alias zfget='noglob zfget' Kamil ^ permalink raw reply [flat|nested] 24+ messages in thread
* Re: Alias call in function fails... 2020-06-23 13:10 ` Kamil Dudka @ 2020-06-23 16:03 ` Bart Schaefer 2020-06-23 21:14 ` Perry Smith 0 siblings, 1 reply; 24+ messages in thread From: Bart Schaefer @ 2020-06-23 16:03 UTC (permalink / raw) To: Zsh Users On Tue, Jun 23, 2020 at 6:11 AM Kamil Dudka <kdudka@redhat.com> wrote: > > On Tuesday, June 23, 2020 2:46:50 PM CEST Perry Smith wrote: > > > > I don’t know of anything an alias can do that a function can’t? > > % alias %=' ' > % alias mkdir='nocorrect mkdir' > % alias zfget='noglob zfget' Yes, that's one of two things alias can do that functions cannot: Give instructions to the parser. The other thing is make replacements in contexts other than the "command position" (global aliases). Both of those are intended as interactive features, though, because there are other ways to do them in scripts, where saving keystrokes and the user's memory are not important. ^ permalink raw reply [flat|nested] 24+ messages in thread
* Re: Alias call in function fails... 2020-06-23 16:03 ` Bart Schaefer @ 2020-06-23 21:14 ` Perry Smith 2020-06-23 22:54 ` Daniel Shahaf 2020-06-24 2:58 ` Grant Taylor 0 siblings, 2 replies; 24+ messages in thread From: Perry Smith @ 2020-06-23 21:14 UTC (permalink / raw) To: Zsh Users [-- Attachment #1: Type: text/plain, Size: 304 bytes --] > On Jun 23, 2020, at 11:03 AM, Bart Schaefer <schaefer@brasslantern.com <mailto:schaefer@brasslantern.com>> wrote: > > The other thing is make replacements in contexts other than the > "command position" (global aliases). I’d really appreciate if you could give a few examples of this. ^ permalink raw reply [flat|nested] 24+ messages in thread
* Re: Alias call in function fails... 2020-06-23 21:14 ` Perry Smith @ 2020-06-23 22:54 ` Daniel Shahaf 2020-06-23 23:29 ` Perry Smith 2020-06-23 23:40 ` Bart Schaefer 2020-06-24 2:58 ` Grant Taylor 1 sibling, 2 replies; 24+ messages in thread From: Daniel Shahaf @ 2020-06-23 22:54 UTC (permalink / raw) To: Perry Smith; +Cc: Zsh Users Perry Smith wrote on Tue, 23 Jun 2020 16:14 -0500: > > On Jun 23, 2020, at 11:03 AM, Bart Schaefer <schaefer@brasslantern.com <mailto:schaefer@brasslantern.com>> wrote: > > > > The other thing is make replacements in contexts other than the > > "command position" (global aliases). > > I’d really appreciate if you could give a few examples of this. > [[[ diff --git a/Doc/Zsh/builtins.yo b/Doc/Zsh/builtins.yo index ada69c99a..bff5c4a18 100644 --- a/Doc/Zsh/builtins.yo +++ b/Doc/Zsh/builtins.yo @@ -105,7 +105,16 @@ For each var(name) with a corresponding var(value), define an alias with that value. A trailing space in var(value) causes the next word to be checked for alias expansion. If the tt(-g) flag is present, define a global alias; global aliases are expanded even if they do not -occur in command position. +occur in command position: + +example(% print -rC1 foo bar ANNOTATE +foo +bar +ANNOTATE +% alias -g ANNOTATE='| nl -ba' +% print -rC1 foo bar ANNOTATE + 1 foo + 2 bar) If the tt(-s) flag is present, define a suffix alias: if the command word on a command line is in the form `var(text)tt(.)var(name)', where ]]] Is this sufficiently clear to be committed? There's already a noderef(Aliasing) a few paragraphs below, which explains how aliases are expanded before almost all other parsing (which is why the «|» on the RHS works), but it's perhaps not self-explanatory that nl(1) is an external command. ^ permalink raw reply [flat|nested] 24+ messages in thread
* Re: Alias call in function fails... 2020-06-23 22:54 ` Daniel Shahaf @ 2020-06-23 23:29 ` Perry Smith 2020-06-23 23:43 ` Bart Schaefer 2020-06-23 23:40 ` Bart Schaefer 1 sibling, 1 reply; 24+ messages in thread From: Perry Smith @ 2020-06-23 23:29 UTC (permalink / raw) To: Daniel Shahaf; +Cc: Zsh Users > On Jun 23, 2020, at 5:54 PM, Daniel Shahaf <d.s@daniel.shahaf.name> wrote: > > Perry Smith wrote on Tue, 23 Jun 2020 16:14 -0500: >>> On Jun 23, 2020, at 11:03 AM, Bart Schaefer <schaefer@brasslantern.com <mailto:schaefer@brasslantern.com>> wrote: >>> >>> The other thing is make replacements in contexts other than the >>> "command position" (global aliases). >> >> I’d really appreciate if you could give a few examples of this. >> > > [[[ > diff --git a/Doc/Zsh/builtins.yo b/Doc/Zsh/builtins.yo > index ada69c99a..bff5c4a18 100644 > --- a/Doc/Zsh/builtins.yo > +++ b/Doc/Zsh/builtins.yo > @@ -105,7 +105,16 @@ For each var(name) with a corresponding var(value), define an alias > with that value. A trailing space in var(value) causes the next word > to be checked for alias expansion. If the tt(-g) flag is present, > define a global alias; global aliases are expanded even if they do not > -occur in command position. > +occur in command position: > + > +example(% print -rC1 foo bar ANNOTATE > +foo > +bar > +ANNOTATE > +% alias -g ANNOTATE='| nl -ba' > +% print -rC1 foo bar ANNOTATE > + 1 foo > + 2 bar) > > If the tt(-s) flag is present, define a suffix alias: if the command > word on a command line is in the form `var(text)tt(.)var(name)', where > ]]] > > Is this sufficiently clear to be committed? There's already a > noderef(Aliasing) a few paragraphs below, which explains how aliases > are expanded before almost all other parsing (which is why the «|» on > the RHS works), but it's perhaps not self-explanatory that nl(1) is an > external command. Ahh… ok. Thank you. I remember reading that now (before your changes) but didn’t really ingest it. ^ permalink raw reply [flat|nested] 24+ messages in thread
* Re: Alias call in function fails... 2020-06-23 23:29 ` Perry Smith @ 2020-06-23 23:43 ` Bart Schaefer 2020-06-24 0:47 ` Perry Smith 0 siblings, 1 reply; 24+ messages in thread From: Bart Schaefer @ 2020-06-23 23:43 UTC (permalink / raw) To: Perry Smith; +Cc: Zsh Users On Tue, Jun 23, 2020 at 4:30 PM Perry Smith <pedz@easesoftware.com> wrote: > > Ahh… ok. Thank you. I remember reading that now (before your changes) > but didn’t really ingest it. The more classic example is something excessively abbreviated such as % alias -g L='|less' % find ~ -name \*.gif L ^ permalink raw reply [flat|nested] 24+ messages in thread
* Re: Alias call in function fails... 2020-06-23 23:43 ` Bart Schaefer @ 2020-06-24 0:47 ` Perry Smith 2020-06-24 9:28 ` Daniel Shahaf 0 siblings, 1 reply; 24+ messages in thread From: Perry Smith @ 2020-06-24 0:47 UTC (permalink / raw) To: Bart Schaefer; +Cc: Zsh Users > On Jun 23, 2020, at 6:43 PM, Bart Schaefer <schaefer@brasslantern.com> wrote: > > On Tue, Jun 23, 2020 at 4:30 PM Perry Smith <pedz@easesoftware.com> wrote: >> >> Ahh… ok. Thank you. I remember reading that now (before your changes) >> but didn’t really ingest it. > > The more classic example is something excessively abbreviated such as > > % alias -g L='|less' > % find ~ -name \*.gif L For me, I think I would more likely do something like: % alias -g opts=“-a -b -c -d” % foo opts path/to/file But, given my relative inexperience with zsh, I not think about alias -g and would likely do: % opts=“-a -b -c -d” % foo $opts path/to/file or use alias -g to save a really long ugly path. etc. ^ permalink raw reply [flat|nested] 24+ messages in thread
* Re: Alias call in function fails... 2020-06-24 0:47 ` Perry Smith @ 2020-06-24 9:28 ` Daniel Shahaf 2020-06-24 12:55 ` Perry Smith 0 siblings, 1 reply; 24+ messages in thread From: Daniel Shahaf @ 2020-06-24 9:28 UTC (permalink / raw) To: Perry Smith; +Cc: Bart Schaefer, Zsh Users Perry Smith wrote on Tue, Jun 23, 2020 at 19:47:05 -0500: > > > > On Jun 23, 2020, at 6:43 PM, Bart Schaefer <schaefer@brasslantern.com> wrote: > > > > On Tue, Jun 23, 2020 at 4:30 PM Perry Smith <pedz@easesoftware.com> wrote: > >> > >> Ahh… ok. Thank you. I remember reading that now (before your changes) > >> but didn’t really ingest it. > > > > The more classic example is something excessively abbreviated such as > > > > % alias -g L='|less' > > % find ~ -name \*.gif L > > For me, I think I would more likely do something like: > > % alias -g opts=“-a -b -c -d” > % foo opts path/to/file > > But, given my relative inexperience with zsh, I not think about alias -g and > would likely do: > > % opts=“-a -b -c -d” > % foo $opts path/to/file > > or use alias -g to save a really long ugly path. etc. > The second example won't work with the default settings (see http://zsh.sourceforge.net/FAQ/zshfaq03.html#l18). The standard workaround is to use an array variable, and I'd like the example to show a use of aliases that can't easily be achieved without them. (This also goes for Grant's example.) Cheers, Daniel > or use alias -g to save a really long ugly path. etc. > ^ permalink raw reply [flat|nested] 24+ messages in thread
* Re: Alias call in function fails... 2020-06-24 9:28 ` Daniel Shahaf @ 2020-06-24 12:55 ` Perry Smith 0 siblings, 0 replies; 24+ messages in thread From: Perry Smith @ 2020-06-24 12:55 UTC (permalink / raw) To: Daniel Shahaf; +Cc: Bart Schaefer, Zsh Users > On Jun 24, 2020, at 4:28 AM, Daniel Shahaf <d.s@daniel.shahaf.name> wrote: > > Perry Smith wrote on Tue, Jun 23, 2020 at 19:47:05 -0500: >> >> >>> On Jun 23, 2020, at 6:43 PM, Bart Schaefer <schaefer@brasslantern.com> wrote: >>> >>> On Tue, Jun 23, 2020 at 4:30 PM Perry Smith <pedz@easesoftware.com> wrote: >>>> >>>> Ahh… ok. Thank you. I remember reading that now (before your changes) >>>> but didn’t really ingest it. >>> >>> The more classic example is something excessively abbreviated such as >>> >>> % alias -g L='|less' >>> % find ~ -name \*.gif L >> >> For me, I think I would more likely do something like: >> >> % alias -g opts=“-a -b -c -d” >> % foo opts path/to/file >> >> But, given my relative inexperience with zsh, I not think about alias -g and >> would likely do: >> >> % opts=“-a -b -c -d” >> % foo $opts path/to/file >> >> or use alias -g to save a really long ugly path. etc. >> > > The second example won't work with the default settings (see > http://zsh.sourceforge.net/FAQ/zshfaq03.html#l18). The standard > workaround is to use an array variable, and I'd like the example to show > a use of aliases that can't easily be achieved without them. (This also > goes for Grant's example.) Ah… yes, you are right of course. It demonstrates that my mind is still using bash. ^ permalink raw reply [flat|nested] 24+ messages in thread
* Re: Alias call in function fails... 2020-06-23 22:54 ` Daniel Shahaf 2020-06-23 23:29 ` Perry Smith @ 2020-06-23 23:40 ` Bart Schaefer 2020-06-24 10:10 ` Daniel Shahaf 1 sibling, 1 reply; 24+ messages in thread From: Bart Schaefer @ 2020-06-23 23:40 UTC (permalink / raw) To: Zsh Users On Tue, Jun 23, 2020 at 3:55 PM Daniel Shahaf <d.s@daniel.shahaf.name> wrote: > > I[...] it's perhaps not self-explanatory that nl(1) is an > external command. You could use "cat -n" instead. ^ permalink raw reply [flat|nested] 24+ messages in thread
* Re: Alias call in function fails... 2020-06-23 23:40 ` Bart Schaefer @ 2020-06-24 10:10 ` Daniel Shahaf 2020-06-24 10:47 ` Daniel Shahaf 0 siblings, 1 reply; 24+ messages in thread From: Daniel Shahaf @ 2020-06-24 10:10 UTC (permalink / raw) To: Bart Schaefer; +Cc: Zsh Users Bart Schaefer wrote on Tue, Jun 23, 2020 at 16:40:04 -0700: > On Tue, Jun 23, 2020 at 3:55 PM Daniel Shahaf <d.s@daniel.shahaf.name> wrote: > > > > I[...] it's perhaps not self-explanatory that nl(1) is an > > external command. > > You could use "cat -n" instead. It's unportable. I'd prefer a runnable example. How about: [[[ diff --git a/Doc/Zsh/builtins.yo b/Doc/Zsh/builtins.yo index ada69c99a..d35cad182 100644 --- a/Doc/Zsh/builtins.yo +++ b/Doc/Zsh/builtins.yo @@ -105,7 +105,13 @@ For each var(name) with a corresponding var(value), define an alias with that value. A trailing space in var(value) causes the next word to be checked for alias expansion. If the tt(-g) flag is present, define a global alias; global aliases are expanded even if they do not -occur in command position. +occur in command position: + +example(% perldoc --help 2>&1 | grep 'built-in functions' + -f Search Perl built-in functions +% alias -g HG='--help 2>&1 | grep' +% perldoc HG 'built-in functions' + -f Search Perl built-in functions) If the tt(-s) flag is present, define a suffix alias: if the command word on a command line is in the form `var(text)tt(.)var(name)', where ]]] The example uses perldoc(1) because that command is an external command, is not system-dependent, and prints usage information to stderr when invoked with --help. There's already an example of «| less» in StartupFiles/zshrc:61. Cheers, Daniel ^ permalink raw reply [flat|nested] 24+ messages in thread
* Re: Alias call in function fails... 2020-06-24 10:10 ` Daniel Shahaf @ 2020-06-24 10:47 ` Daniel Shahaf 0 siblings, 0 replies; 24+ messages in thread From: Daniel Shahaf @ 2020-06-24 10:47 UTC (permalink / raw) To: Zsh Users; +Cc: Bart Schaefer Daniel Shahaf wrote on Wed, 24 Jun 2020 10:10 +0000: > Bart Schaefer wrote on Tue, Jun 23, 2020 at 16:40:04 -0700: > > On Tue, Jun 23, 2020 at 3:55 PM Daniel Shahaf <d.s@daniel.shahaf.name> wrote: > > > > > > I[...] it's perhaps not self-explanatory that nl(1) is an > > > external command. > > > > You could use "cat -n" instead. > > It's unportable. I'd prefer a runnable example. > > How about: Extended: 8<--8<-- From 4e1d6b29d42662eb9b6f5d1cf7dc3900c3142762 Mon Sep 17 00:00:00 2001 From: Daniel Shahaf <d.s@daniel.shahaf.name> Date: Wed, 24 Jun 2020 10:40:23 +0000 Subject: [PATCH 1/2] Extend documentation of global aliases. --- Doc/Zsh/builtins.yo | 8 +++++++- Doc/Zsh/grammar.yo | 9 +++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/Doc/Zsh/builtins.yo b/Doc/Zsh/builtins.yo index ada69c99a..d35cad182 100644 --- a/Doc/Zsh/builtins.yo +++ b/Doc/Zsh/builtins.yo @@ -105,7 +105,13 @@ For each var(name) with a corresponding var(value), define an alias with that value. A trailing space in var(value) causes the next word to be checked for alias expansion. If the tt(-g) flag is present, define a global alias; global aliases are expanded even if they do not -occur in command position. +occur in command position: + +example(% perldoc --help 2>&1 | grep 'built-in functions' + -f Search Perl built-in functions +% alias -g HG='--help 2>&1 | grep' +% perldoc HG 'built-in functions' + -f Search Perl built-in functions) If the tt(-s) flag is present, define a suffix alias: if the command word on a command line is in the form `var(text)tt(.)var(name)', where diff --git a/Doc/Zsh/grammar.yo b/Doc/Zsh/grammar.yo index a4e0c1121..7eabd75ce 100644 --- a/Doc/Zsh/grammar.yo +++ b/Doc/Zsh/grammar.yo @@ -597,6 +597,15 @@ word, e.g. tt(\foo). Any form of quoting works, although there is nothing to prevent an alias being defined for the quoted form such as tt(\foo) as well. +In particular, note that quoting must be used when using tt(unalias) to remove +global aliases: + +example(% alias -g foo=bar +% unalias foo +unalias: no such hash table element: bar +% unalias \foo +% ) + When tt(POSIX_ALIASES) is set, only plain unquoted strings are eligible for aliasing. The tt(alias) builtin does not reject ineligible aliases, but they are not expanded. From c554b79da628ea7eb96ff37ddaa456b07004bfe8 Mon Sep 17 00:00:00 2001 From: Daniel Shahaf <d.s@daniel.shahaf.name> Date: Wed, 24 Jun 2020 10:40:45 +0000 Subject: [PATCH 2/2] Update aliases documentation for the addition of the ALIAS_FUNC_DEF option. --- Doc/Zsh/grammar.yo | 25 +++++-------------------- 1 file changed, 5 insertions(+), 20 deletions(-) diff --git a/Doc/Zsh/grammar.yo b/Doc/Zsh/grammar.yo index 7eabd75ce..2eb2018d2 100644 --- a/Doc/Zsh/grammar.yo +++ b/Doc/Zsh/grammar.yo @@ -571,6 +571,11 @@ position (if it could be the first word of a simple command), or if the alias is global. If the replacement text ends with a space, the next word in the shell input is always eligible for purposes of alias expansion. + +It is an error for the function name, var(word), in the sh-compatible function +definition syntax `var(word) tt(+LPAR()+RPAR()) ...' to be a word that resulted +from alias expansion, unless the tt(ALIAS_FUNC_DEF) option is set. + findex(alias, use of) cindex(aliases, global) An alias is defined using the tt(alias) builtin; global aliases @@ -656,26 +661,6 @@ a problem in shell scripts, functions, and code executed with `tt(source)' or `tt(.)'. Consequently, use of functions rather than aliases is recommended in non-interactive code. -Note also the unhelpful interaction of aliases and function definitions: - -example(alias func='noglob func' -func+LPAR()RPAR() { - echo Do something with $* -}) - -Because aliases are expanded in function definitions, this causes the -following command to be executed: - -example(noglob func+LPAR()RPAR() { - echo Do something with $* -}) - -which defines tt(noglob) as well as tt(func) as functions with the -body given. To avoid this, either quote the name tt(func) or use the -alternative function definition form `tt(function func)'. Ensuring the -alias is defined after the function works but is problematic if the -code fragment might be re-executed. - texinode(Quoting)()(Aliasing)(Shell Grammar) sect(Quoting) cindex(quoting) ^ permalink raw reply [flat|nested] 24+ messages in thread
* Re: Alias call in function fails... 2020-06-23 21:14 ` Perry Smith 2020-06-23 22:54 ` Daniel Shahaf @ 2020-06-24 2:58 ` Grant Taylor 1 sibling, 0 replies; 24+ messages in thread From: Grant Taylor @ 2020-06-24 2:58 UTC (permalink / raw) To: zsh-users [-- Attachment #1: Type: text/plain, Size: 991 bytes --] On 6/23/20 3:14 PM, Perry Smith wrote: > I’d really appreciate if you could give a few examples of this. Here's a different example of how I used a global (-g) alias: % alias -g PTBONE="-R 127.0.0.1:3128:127.0.0.1:3128" % ssh PTBONE host.example.net PTBONE is an alias that has the long string of options substituted in it's place. I chose a global alias because I rarely wanted the PTBONE configuration on my ssh connections. But I wanted it often enough that typing out the full thing was a PITA. I felt like adding PTBONE /when/ I wanted it was a reasonable compromise. Backstory: I was periodically updating a bunch of Red Hat systems that were configured to use a proxy on localhost (127.0.0.1 to avoid ::1 confusion). Said proxy was created by OpenSSH's remote port forwarding back to a local (caching) proxy like Squid. PTBONE is short for Proxy Trombone, as a reference / nod to what the traffic does. -- Grant. . . . unix || die [-- Attachment #2: S/MIME Cryptographic Signature --] [-- Type: application/pkcs7-signature, Size: 4013 bytes --] ^ permalink raw reply [flat|nested] 24+ messages in thread
* Re: Alias call in function fails... 2020-06-22 23:23 ` Mikael Magnusson 2020-06-23 8:37 ` Andreas Kusalananda Kähäri @ 2020-06-29 16:24 ` Sebastian Gniazdowski 2020-06-29 16:55 ` Bart Schaefer 1 sibling, 1 reply; 24+ messages in thread From: Sebastian Gniazdowski @ 2020-06-29 16:24 UTC (permalink / raw) To: Mikael Magnusson; +Cc: Frank Gallacher, zsh-users [-- Attachment #1: Type: text/plain, Size: 1184 bytes --] I once was testing with Eric on IRC and the aliases sometimes do work within the same file. I think that the distance between the definition and usage is important. I wonder why? Are the scripts (zshrc in this case) being parsed in an advancing manner? On Tue, 23 Jun 2020 at 01:25, Mikael Magnusson <mikachu@gmail.com> wrote: > On 6/22/20, Frank Gallacher <franxg@gmail.com> wrote: > > Greetings, > > > > I have a function: > > > [...] > > > > Which calls an alias: > > > > alias dumpit='hexdump -C -n 128' > > > > It works fine in bash, but with zsh now I get: > > > > :::::::: test.txt :::::::: > > dumpall:11: command not found: dumpit > > > > Am I doing something wrong??? > > Yes, don't use aliases for anything but interactive usage. Eg if you > want to use dumpit in a script, make it be a function: > dumpit() { hexdump -C -n 128 } > > Aliases are expanded on parse time, which means aliases defined in a > file won't be usable in that same file. (Because it is parsed in its > entirety before any of the code is actually run). > > -- > Mikael Magnusson > -- Sebastian Gniazdowski IRC: https://kiwiirc.com/client/chat.freenode.net:+6697/#zinit Blog: http://zdharma.org ^ permalink raw reply [flat|nested] 24+ messages in thread
* Re: Alias call in function fails... 2020-06-29 16:24 ` Sebastian Gniazdowski @ 2020-06-29 16:55 ` Bart Schaefer 2020-06-30 4:02 ` Bart Schaefer 0 siblings, 1 reply; 24+ messages in thread From: Bart Schaefer @ 2020-06-29 16:55 UTC (permalink / raw) To: zsh-users; +Cc: Frank Gallacher On Mon, Jun 29, 2020 at 9:26 AM Sebastian Gniazdowski <sgniazdowski@gmail.com> wrote: > > I once was testing with Eric on IRC and the aliases sometimes do work > within the same file. I think that the distance between the definition and > usage is important. No, that's unrelated. Code structure and execution context are everything. As Peter explained, initialization files, sourced files, and scripts (that are not function bodies) are executed one newline-separated command at a time (Peter said "line by line" but that's not precisely correct **), whereas function definitions (no matter where they appear, but especially autoloads) are fully parsed before being executed. If you think about it, that latter must be true, otherwise you'd run the function instantly every time you tried to define one. Where you're probably becoming confused is "one newline-separated command at a time". Pipelines, commands joined with ";" or "&&" or "||", and structured commands such as an if/else cascade or a while loop, are all, like a function body, fully parsed before being executed. Even a sequence of commands simply enclosed in braces is fully parsed before being executed. Thus even in an interactive shell: % { alias -g foo=bar cursh> echo foo } foo % echo foo bar % alias -g foo=again ; echo foo bar % echo foo again You have to be certain that the alias command is actually executed, NOT merely seen by the parser, before the first usage of that alias is seen by the parser. Run your confusing script with "setopt xtrace" and watch for the order of execution. ^ permalink raw reply [flat|nested] 24+ messages in thread
* Re: Alias call in function fails... 2020-06-29 16:55 ` Bart Schaefer @ 2020-06-30 4:02 ` Bart Schaefer 0 siblings, 0 replies; 24+ messages in thread From: Bart Schaefer @ 2020-06-30 4:02 UTC (permalink / raw) To: zsh-users; +Cc: Frank Gallacher On Mon, Jun 29, 2020 at 9:55 AM Bart Schaefer <schaefer@brasslantern.com> wrote: > > As Peter explained, initialization files, sourced files, > and scripts (that are not function bodies) are executed one > newline-separated command at a time (Peter said "line by line" but > that's not precisely correct **) Just realized I never completed my footnote. ** Csh notoriously executed even loops and conditionals one command at a time, and in the case of loops would rewind the input file seek pointer to the start of the loop and execute it again until the loop condition became false. I wrote some entertaining self-modifying csh scripts back in the day. Anyway that's one reason why aliases in csh behave differently. ^ permalink raw reply [flat|nested] 24+ messages in thread
end of thread, other threads:[~2020-06-30 4:03 UTC | newest] Thread overview: 24+ messages (download: mbox.gz / follow: Atom feed) -- links below jump to the message on this page -- 2020-06-22 12:49 Alias call in function fails Frank Gallacher 2020-06-22 23:23 ` Mikael Magnusson 2020-06-23 8:37 ` Andreas Kusalananda Kähäri 2020-06-23 9:14 ` Peter Stephenson 2020-06-23 9:55 ` Mikael Magnusson 2020-06-23 11:28 ` Andreas Kusalananda Kähäri 2020-06-23 12:04 ` Daniel Shahaf 2020-06-23 12:46 ` Perry Smith 2020-06-23 13:10 ` Kamil Dudka 2020-06-23 16:03 ` Bart Schaefer 2020-06-23 21:14 ` Perry Smith 2020-06-23 22:54 ` Daniel Shahaf 2020-06-23 23:29 ` Perry Smith 2020-06-23 23:43 ` Bart Schaefer 2020-06-24 0:47 ` Perry Smith 2020-06-24 9:28 ` Daniel Shahaf 2020-06-24 12:55 ` Perry Smith 2020-06-23 23:40 ` Bart Schaefer 2020-06-24 10:10 ` Daniel Shahaf 2020-06-24 10:47 ` Daniel Shahaf 2020-06-24 2:58 ` Grant Taylor 2020-06-29 16:24 ` Sebastian Gniazdowski 2020-06-29 16:55 ` Bart Schaefer 2020-06-30 4:02 ` Bart Schaefer
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).