zsh-users
 help / color / mirror / code / Atom feed
* alias/anon_function quoting issue
@ 2023-07-14 14:46 Jim
  2023-07-14 15:06 ` Roman Perepelitsa
                   ` (2 more replies)
  0 siblings, 3 replies; 7+ messages in thread
From: Jim @ 2023-07-14 14:46 UTC (permalink / raw)
  To: zsh

[-- Attachment #1: Type: text/plain, Size: 1220 bytes --]

Hi everyone,
First, admittedly what I'm trying to do can be done in a regular function
without
an issue. That said, I just hate giving up trying to make it work.(sorry)

In my .aliases file I have a number of alias/anon_functions defined.  This
is so I can pass
an arg using an alias, as an alias can not accept arguments. This normally
works well,
but I ran into an issue when within the anon_function I tried to set a
parameter with
ansi data.

e.g. Var=$'\e['

When trying to read in the aliases form my .aliases file I get the
following:

. ~/.aliases
/home/user/.aliases:<line no>: bad pattern: name=() <some code> Var=$\e['

Have tried a number of different quoting changes, like:
    "\" in front of single quotes
    using double quotes instead of single quotes in different places
      (not sure if I was doing this correctly ... but tried anyway)

So far everything I have tried hasn't worked.

FYI, the alias itself is using single quotes to surround the anon_function.
alias name='(){
   ...
}'

Q:  Is something like this possible?
      If so, does anyone have an idea how to do this?

Note:  This is not critical, just annoying that "I" can't figure it out.

Thanks, and best regards to all,

Jim Murphy

[-- Attachment #2: Type: text/html, Size: 1768 bytes --]

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: alias/anon_function quoting issue
  2023-07-14 14:46 alias/anon_function quoting issue Jim
@ 2023-07-14 15:06 ` Roman Perepelitsa
  2023-07-14 16:42 ` Bart Schaefer
  2023-07-27  8:50 ` Johan Grande
  2 siblings, 0 replies; 7+ messages in thread
From: Roman Perepelitsa @ 2023-07-14 15:06 UTC (permalink / raw)
  To: linuxtechguy; +Cc: zsh

On Fri, Jul 14, 2023 at 4:48 PM Jim <linux.tech.guy@gmail.com> wrote:
>
> e.g. Var=$'\e['

You don't need to quote the dollar sign within single quotes. The
single quote itself can be quoted by replacing it with '\'': the first
single quote closes the open one, \' inserts a literal single quote,
and the last single quote reopens the string literal.

    alias name='() {
      Var=$'\''\e['\''
    }'

Roman.


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: alias/anon_function quoting issue
  2023-07-14 14:46 alias/anon_function quoting issue Jim
  2023-07-14 15:06 ` Roman Perepelitsa
@ 2023-07-14 16:42 ` Bart Schaefer
  2023-07-14 16:52   ` Bart Schaefer
  2023-07-14 21:24   ` Jim
  2023-07-27  8:50 ` Johan Grande
  2 siblings, 2 replies; 7+ messages in thread
From: Bart Schaefer @ 2023-07-14 16:42 UTC (permalink / raw)
  To: linuxtechguy; +Cc: zsh

On Fri, Jul 14, 2023 at 7:47 AM Jim <linux.tech.guy@gmail.com> wrote:
>
> Note:  This is not critical, just annoying that "I" can't figure it out.

Here's a little trick for making the shell figure it out for you.

Step 1:  Define the function you want using a placeholder name of some sort:

    function foo { Var=$'\e[' }

Step 2:  Define the alias by referencing the $functions special
parameter (which contains only the body of each function):

    alias foo="() { $functions[foo] }"

Step 3:  Delete the placeholder function.

    unfunction foo

Step 4 (if you want):  Confirm the alias definition:

    alias foo

The shell will use a bit more elaborate quoting than strictly
necessary in some cases (and for some reason $functions encodes the
body with a leading tab character, removing that if you want is left
as an exercise) but you shouldn't need to worry about it working.

I can't think of a reason to define a global alias as a function, but
be sure the placeholder name used at steps 1 and 3 is not a global
alias.


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: alias/anon_function quoting issue
  2023-07-14 16:42 ` Bart Schaefer
@ 2023-07-14 16:52   ` Bart Schaefer
  2023-07-14 21:24   ` Jim
  1 sibling, 0 replies; 7+ messages in thread
From: Bart Schaefer @ 2023-07-14 16:52 UTC (permalink / raw)
  To: linuxtechguy; +Cc: zsh

On Fri, Jul 14, 2023 at 9:42 AM Bart Schaefer <schaefer@brasslantern.com> wrote:
>
>     alias foo
>
> The shell will use a bit more elaborate quoting than strictly
> necessary in some cases

To clarify that:  The output of the "alias" command will use elaborate
quoting, not the internal representation of the alias.  You can see it
in a less convoluted form by examining the $alliases parameter:

% print -r -- $aliases[foo]
() {     Var=$'\e['  }

Or to get what you were originally after, single quoting:

% print -r -- ${(qq)aliases[foo]}
'() {     Var=$'\''\e['\''  }'


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: alias/anon_function quoting issue
  2023-07-14 16:42 ` Bart Schaefer
  2023-07-14 16:52   ` Bart Schaefer
@ 2023-07-14 21:24   ` Jim
  1 sibling, 0 replies; 7+ messages in thread
From: Jim @ 2023-07-14 21:24 UTC (permalink / raw)
  To: Bart Schaefer; +Cc: zsh

[-- Attachment #1: Type: text/plain, Size: 279 bytes --]

On Fri, Jul 14, 2023 at 11:42 AM Bart Schaefer <schaefer@brasslantern.com>
wrote:

>
> Here's a little trick for making the shell figure it out for you.
>

Another reason to love Z-Shell.  Very nice trick.  Learned my something
new for the day.  :-)

Thanks,

Jim

[-- Attachment #2: Type: text/html, Size: 700 bytes --]

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: alias/anon_function quoting issue
  2023-07-14 14:46 alias/anon_function quoting issue Jim
  2023-07-14 15:06 ` Roman Perepelitsa
  2023-07-14 16:42 ` Bart Schaefer
@ 2023-07-27  8:50 ` Johan Grande
  2023-07-28 21:53   ` Bart Schaefer
  2 siblings, 1 reply; 7+ messages in thread
From: Johan Grande @ 2023-07-27  8:50 UTC (permalink / raw)
  To: linuxtechguy, zsh

Le 14/07/2023 à 16:46, Jim a écrit :
> Hi everyone, First, admittedly what I'm trying to do can be done in a
> regular function without an issue. That said, I just hate giving up
> trying to make it work.(sorry)
> 
> [...]
> 
> FYI, the alias itself is using single quotes to surround the
> anon_function. alias name='(){ ... }'

Hi, I'm curious, is there a particular advantage to doing this rather 
than declaring a function?

-- 
Johan


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: alias/anon_function quoting issue
  2023-07-27  8:50 ` Johan Grande
@ 2023-07-28 21:53   ` Bart Schaefer
  0 siblings, 0 replies; 7+ messages in thread
From: Bart Schaefer @ 2023-07-28 21:53 UTC (permalink / raw)
  To: Johan Grande; +Cc: linuxtechguy, zsh

On Thu, Jul 27, 2023 at 1:51 AM Johan Grande <nahoj@crans.org> wrote:
>
> Le 14/07/2023 à 16:46, Jim a écrit :
> >
> > alias name='(){ ... }'
>
> Hi, I'm curious, is there a particular advantage to doing this rather
> than declaring a function?

I can think of only a couple.

First, you can define a suffix alias this way:

alias -s tgz='() { tar -tzf - < $1 }'

Second, with an alias you can use simple quoting to switch back to a
command of the same name.  In this example:

vim() { gvim "$@" }
\vim foo

the "vim" function is still executed and you get gvim.  But with this:

alias vim='() { gvim "$@" }'
\vim foo

the alias is not expanded and /bin/vim runs directly.

If there are other reasons they don't occur to me offhand.


^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2023-07-28 21:54 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-07-14 14:46 alias/anon_function quoting issue Jim
2023-07-14 15:06 ` Roman Perepelitsa
2023-07-14 16:42 ` Bart Schaefer
2023-07-14 16:52   ` Bart Schaefer
2023-07-14 21:24   ` Jim
2023-07-27  8:50 ` Johan Grande
2023-07-28 21:53   ` 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).