zsh-users
 help / color / mirror / code / Atom feed
* is variable with variable name possible?
@ 2011-04-04 13:37 zzapper
  2011-04-04 13:48 ` Jérémie Roquet
  0 siblings, 1 reply; 18+ messages in thread
From: zzapper @ 2011-04-04 13:37 UTC (permalink / raw)
  To: zsh-users



somevar=$(pwd)

but I want somevar to be a variable name

-- 
zzapper
http://zzapper.co.uk/ Technical Tips


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

* Re: is variable with variable name possible?
  2011-04-04 13:37 is variable with variable name possible? zzapper
@ 2011-04-04 13:48 ` Jérémie Roquet
  2011-04-04 13:51   ` Jérémie Roquet
  2011-04-04 14:06   ` Stephane Chazelas
  0 siblings, 2 replies; 18+ messages in thread
From: Jérémie Roquet @ 2011-04-04 13:48 UTC (permalink / raw)
  To: Zsh Users; +Cc: zzapper

Hi,

2011/4/4 zzapper <david@tvis.co.uk>:
> somevar=$(pwd)
>
> but I want somevar to be a variable name

$ eval "$somevar=$(pwd)"

Best regards,

-- 
Jérémie


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

* Re: is variable with variable name possible?
  2011-04-04 13:48 ` Jérémie Roquet
@ 2011-04-04 13:51   ` Jérémie Roquet
  2011-04-04 14:13     ` zzapper
  2011-04-04 14:26     ` Bart Schaefer
  2011-04-04 14:06   ` Stephane Chazelas
  1 sibling, 2 replies; 18+ messages in thread
From: Jérémie Roquet @ 2011-04-04 13:51 UTC (permalink / raw)
  To: Zsh Users; +Cc: zzapper

2011/4/4 Jérémie Roquet <arkanosis@gmail.com>:
> 2011/4/4 zzapper <david@tvis.co.uk>:
>> somevar=$(pwd)
>>
>> but I want somevar to be a variable name
>
> $ eval "$somevar=$(pwd)"

Actually, the quotes aren't even needed:

$ eval $somevar=$(pwd)

-- 
Jérémie


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

* Re: is variable with variable name possible?
  2011-04-04 13:48 ` Jérémie Roquet
  2011-04-04 13:51   ` Jérémie Roquet
@ 2011-04-04 14:06   ` Stephane Chazelas
  2011-04-04 14:25     ` Mikael Magnusson
  2011-04-04 14:33     ` Jérémie Roquet
  1 sibling, 2 replies; 18+ messages in thread
From: Stephane Chazelas @ 2011-04-04 14:06 UTC (permalink / raw)
  To: Jérémie Roquet; +Cc: Zsh Users, zzapper

2011-04-04 15:48:32 +0200, Jérémie Roquet:
[...]
> 2011/4/4 zzapper <david@tvis.co.uk>:
> > somevar=$(pwd)
> >
> > but I want somevar to be a variable name
> 
> $ eval "$somevar=$(pwd)"
[...]

No!

eval "$somevar=\$(pwd)"
or
eval "$somevar=\$PWD"


Or think of what would happen if $PWD was "/tmp/x;rm -rf /home"

-- 
Stephane


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

* Re: is variable with variable name possible?
  2011-04-04 13:51   ` Jérémie Roquet
@ 2011-04-04 14:13     ` zzapper
  2011-04-04 14:26     ` Bart Schaefer
  1 sibling, 0 replies; 18+ messages in thread
From: zzapper @ 2011-04-04 14:13 UTC (permalink / raw)
  To: zsh-users

=?UTF-8?B?SsOpcsOpbWllIFJvcXVldA==?= wrote in 
news:BANLkTikVWk9t4h3APSkFN7tOKyjTHJgmJg@mail.gmail.com:

> eval "$somevar=$(pwd)"
> 

Thanks, that works fine, and I already knww that but just had a memory 
glitch!!

-- 
zzapper
http://zzapper.co.uk/ Technical Tips


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

* Re: is variable with variable name possible?
  2011-04-04 14:06   ` Stephane Chazelas
@ 2011-04-04 14:25     ` Mikael Magnusson
  2011-04-04 16:28       ` Stephane Chazelas
  2011-04-04 14:33     ` Jérémie Roquet
  1 sibling, 1 reply; 18+ messages in thread
From: Mikael Magnusson @ 2011-04-04 14:25 UTC (permalink / raw)
  To: Zsh Users

On 4 April 2011 16:06, Stephane Chazelas <stephane_chazelas@yahoo.fr> wrote:
> 2011-04-04 15:48:32 +0200, Jérémie Roquet:
> [...]
>> 2011/4/4 zzapper <david@tvis.co.uk>:
>> > somevar=$(pwd)
>> >
>> > but I want somevar to be a variable name
>>
>> $ eval "$somevar=$(pwd)"
> [...]
>
> No!
>
> eval "$somevar=\$(pwd)"
> or
> eval "$somevar=\$PWD"
>
>
> Or think of what would happen if $PWD was "/tmp/x;rm -rf /home"

How do you feel about this?
: ${(P)somevar::=$PWD}

-- 
Mikael Magnusson


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

* Re: is variable with variable name possible?
  2011-04-04 13:51   ` Jérémie Roquet
  2011-04-04 14:13     ` zzapper
@ 2011-04-04 14:26     ` Bart Schaefer
  2011-04-05  8:31       ` Bart Schaefer
  1 sibling, 1 reply; 18+ messages in thread
From: Bart Schaefer @ 2011-04-04 14:26 UTC (permalink / raw)
  To: Zsh Users

On Apr 4,  3:51pm, Jeremie Roquet wrote:
} Subject: Re: is variable with variable name possible?
}
} 2011/4/4 Jeremie Roquet <arkanosis@gmail.com>:
} > 2011/4/4 zzapper <david@tvis.co.uk>:
} >> somevar=$(pwd)
} >>
} >> but I want somevar to be a variable name
} >
} > $ eval "$somevar=$(pwd)"
} 
} Actually, the quotes aren't even needed:
} 
} $ eval $somevar=$(pwd)

If $(pwd) has spaces in the directory name, this is going to fail.  So
you do need quotes, but a different kind:

    eval $somevar='$(pwd)'

For scalars (but not array assignments) you can also do

    typeset -g $somevar=$(pwd)

You can also use ${somevar::=$(pwd)} anywhere that you'd reference the
value of $somevar, including the no-op command:

    : ${somevar::=$(pwd)}

That also works for array assignments if you explicitly split:

    : ${(As:/:)somevar::=$(pwd)}
    print -l $somevar

Note though that here the stuff after the = is joined with the first
character of $IFS before being split, so to copy one array to another
you have to be careful.  Also note that without the (A) the split is
applied to the expansion after the assignment.


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

* Re: is variable with variable name possible?
  2011-04-04 14:06   ` Stephane Chazelas
  2011-04-04 14:25     ` Mikael Magnusson
@ 2011-04-04 14:33     ` Jérémie Roquet
  1 sibling, 0 replies; 18+ messages in thread
From: Jérémie Roquet @ 2011-04-04 14:33 UTC (permalink / raw)
  To: Zsh Users; +Cc: Stephane Chazelas, zzapper

2011/4/4 Stephane Chazelas <stephane_chazelas@yahoo.fr>:
> 2011-04-04 15:48:32 +0200, Jérémie Roquet:
> [...]
>> 2011/4/4 zzapper <david@tvis.co.uk>:
>> > somevar=$(pwd)
>> >
>> > but I want somevar to be a variable name
>>
>> $ eval "$somevar=$(pwd)"
> [...]
>
> No!
>
> eval "$somevar=\$(pwd)"
> or
> eval "$somevar=\$PWD"
>
>
> Or think of what would happen if $PWD was "/tmp/x;rm -rf /home"

Oh, right!

-- 
Jérémie


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

* Re: is variable with variable name possible?
  2011-04-04 14:25     ` Mikael Magnusson
@ 2011-04-04 16:28       ` Stephane Chazelas
  2011-04-04 16:34         ` Mikael Magnusson
  0 siblings, 1 reply; 18+ messages in thread
From: Stephane Chazelas @ 2011-04-04 16:28 UTC (permalink / raw)
  To: Mikael Magnusson; +Cc: Zsh Users

2011-04-04 16:25:01 +0200, Mikael Magnusson:
> On 4 April 2011 16:06, Stephane Chazelas <stephane_chazelas@yahoo.fr> wrote:
> > 2011-04-04 15:48:32 +0200, Jérémie Roquet:
> > [...]
> >> 2011/4/4 zzapper <david@tvis.co.uk>:
> >> > somevar=$(pwd)
> >> >
> >> > but I want somevar to be a variable name
> >>
> >> $ eval "$somevar=$(pwd)"
> > [...]
> >
> > No!
> >
> > eval "$somevar=\$(pwd)"
> > or
> > eval "$somevar=\$PWD"
> >
> >
> > Or think of what would happen if $PWD was "/tmp/x;rm -rf /home"
> 
> How do you feel about this?
> : ${(P)somevar::=$PWD}
[...]

Try after

sudo ln -s . '/*'
cd '/*/*/*/*/*'
setopt globsubst

;-)

(if you're lucky, it won't crash your machine).

-- 
Stephane


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

* Re: is variable with variable name possible?
  2011-04-04 16:28       ` Stephane Chazelas
@ 2011-04-04 16:34         ` Mikael Magnusson
  2011-04-04 20:50           ` Stephane Chazelas
  0 siblings, 1 reply; 18+ messages in thread
From: Mikael Magnusson @ 2011-04-04 16:34 UTC (permalink / raw)
  To: Zsh Users

On 4 April 2011 18:28, Stephane Chazelas <stephane_chazelas@yahoo.fr> wrote:
> 2011-04-04 16:25:01 +0200, Mikael Magnusson:
>> On 4 April 2011 16:06, Stephane Chazelas <stephane_chazelas@yahoo.fr> wrote:
>> > 2011-04-04 15:48:32 +0200, Jérémie Roquet:
>> > [...]
>> >> 2011/4/4 zzapper <david@tvis.co.uk>:
>> >> > somevar=$(pwd)
>> >> >
>> >> > but I want somevar to be a variable name
>> >>
>> >> $ eval "$somevar=$(pwd)"
>> > [...]
>> >
>> > No!
>> >
>> > eval "$somevar=\$(pwd)"
>> > or
>> > eval "$somevar=\$PWD"
>> >
>> >
>> > Or think of what would happen if $PWD was "/tmp/x;rm -rf /home"
>>
>> How do you feel about this?
>> : ${(P)somevar::=$PWD}
> [...]
>
> Try after
>
> sudo ln -s . '/*'
> cd '/*/*/*/*/*'
> setopt globsubst
>
> ;-)
>
> (if you're lucky, it won't crash your machine).

Pretty sure globsubst breaks your idea too, but fine,
: ${(P)~~somevar::=$PWD}
In fact, globsubst pretty much breaks every hook function i have :P.

-- 
Mikael Magnusson


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

* Re: is variable with variable name possible?
  2011-04-04 16:34         ` Mikael Magnusson
@ 2011-04-04 20:50           ` Stephane Chazelas
  2011-04-04 22:32             ` Mikael Magnusson
                               ` (3 more replies)
  0 siblings, 4 replies; 18+ messages in thread
From: Stephane Chazelas @ 2011-04-04 20:50 UTC (permalink / raw)
  To: Mikael Magnusson; +Cc: Zsh Users

2011-04-04 18:34:26 +0200, Mikael Magnusson:
[...]
> >> > eval "$somevar=\$(pwd)"
> >> > or
> >> > eval "$somevar=\$PWD"
[...]
> >> How do you feel about this?
> >> : ${(P)somevar::=$PWD}
> > [...]
> >
> > Try after
> >
> > sudo ln -s . '/*'
> > cd '/*/*/*/*/*'
> > setopt globsubst
> >
> > ;-)
> >
> > (if you're lucky, it won't crash your machine).
> 
> Pretty sure globsubst breaks your idea too, but fine,
> : ${(P)~~somevar::=$PWD}
> In fact, globsubst pretty much breaks every hook function i have :P.
[...]

eval "$somevar=\$(pwd)"
or
eval "$somevar=\$PWD"

are POSIX and POSIX shells have globsubst and shwordsplit on.
The above will work regardless of the status of those options.

In POSIX shells, you need to quote variables to prevent
globsubst or shwordsplit, even in arguments to ":" because
globsubst can be very very resource intensive.

You can bring a machine to its knees with

: ${var=foo}

if $var is for instance /*/*/*/../../../*/*/*/../../../*/*/*

So, you should either use instead:

var=${var-foo}
or
: "${var=foo}"

In your example, if we have to accomodate users enabling
shwordsplit or globsubst (or running zsh in sh or ksh
emulation), we need

: "${(P)somevar::=$PWD}"

But

eval "$somevar=\$PWD"

is more legible and more portable.

-- 
Stephane




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

* Re: is variable with variable name possible?
  2011-04-04 20:50           ` Stephane Chazelas
@ 2011-04-04 22:32             ` Mikael Magnusson
  2011-04-05 18:39               ` Stephane Chazelas
  2011-04-05  2:54             ` Aaron Davies
                               ` (2 subsequent siblings)
  3 siblings, 1 reply; 18+ messages in thread
From: Mikael Magnusson @ 2011-04-04 22:32 UTC (permalink / raw)
  To: Zsh Users

On 4 April 2011 22:50, Stephane Chazelas <stephane_chazelas@yahoo.fr> wrote:
> 2011-04-04 18:34:26 +0200, Mikael Magnusson:
> [...]
>> >> > eval "$somevar=\$(pwd)"
>> >> > or
>> >> > eval "$somevar=\$PWD"
> [...]
>> >> How do you feel about this?
>> >> : ${(P)somevar::=$PWD}
>> > [...]
>> >
>> > Try after
>> >
>> > sudo ln -s . '/*'
>> > cd '/*/*/*/*/*'
>> > setopt globsubst
>> >
>> > ;-)
>> >
>> > (if you're lucky, it won't crash your machine).
>>
>> Pretty sure globsubst breaks your idea too, but fine,
>> : ${(P)~~somevar::=$PWD}
>> In fact, globsubst pretty much breaks every hook function i have :P.
> [...]
>
> eval "$somevar=\$(pwd)"
> or
> eval "$somevar=\$PWD"
>
> are POSIX and POSIX shells have globsubst and shwordsplit on.
> The above will work regardless of the status of those options.
>
> In POSIX shells, you need to quote variables to prevent
> globsubst or shwordsplit, even in arguments to ":" because
> globsubst can be very very resource intensive.
>
> You can bring a machine to its knees with
>
> : ${var=foo}
>
> if $var is for instance /*/*/*/../../../*/*/*/../../../*/*/*
>
> So, you should either use instead:
>
> var=${var-foo}
> or
> : "${var=foo}"
>
> In your example, if we have to accomodate users enabling
> shwordsplit or globsubst (or running zsh in sh or ksh
> emulation), we need
>
> : "${(P)somevar::=$PWD}"
>
> But
>
> eval "$somevar=\$PWD"
>
> is more legible and more portable.

But your quotes do nothing (assuming $somevar has a sane value), did you mean
eval $somevar=\"\$PWD\"
or possibly more readable version
eval $somevar='"$PWD"'
? AFAICT, it works without any quotes with globsubst and shwordsplit
set too though, as long as you quote the $ in $PWD, tried in both zsh
and bash.

-- 
Mikael Magnusson


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

* Re: is variable with variable name possible?
  2011-04-04 20:50           ` Stephane Chazelas
  2011-04-04 22:32             ` Mikael Magnusson
@ 2011-04-05  2:54             ` Aaron Davies
  2011-04-05  3:54               ` Phil Pennock
  2011-04-05  4:04             ` ZyX
  2011-04-05  4:10             ` ZyX
  3 siblings, 1 reply; 18+ messages in thread
From: Aaron Davies @ 2011-04-05  2:54 UTC (permalink / raw)
  To: Zsh Users

> eval "$somevar=\$PWD"

shirley we have an equiv of bash2's ${!foo}

i've always advertised zsh as a superset of everything interesting
from every other shell, please tell me i'm not wrong....


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

* Re: is variable with variable name possible?
  2011-04-05  2:54             ` Aaron Davies
@ 2011-04-05  3:54               ` Phil Pennock
  0 siblings, 0 replies; 18+ messages in thread
From: Phil Pennock @ 2011-04-05  3:54 UTC (permalink / raw)
  To: Aaron Davies; +Cc: Zsh Users

On 2011-04-04 at 22:54 -0400, Aaron Davies wrote:
> > eval "$somevar=\$PWD"
> 
> shirley we have an equiv of bash2's ${!foo}

Sure thing, and just like Bash, it's useful for de-referencing more than
it is for assignment.

${(P)foo}

> i've always advertised zsh as a superset of everything interesting
> from every other shell, please tell me i'm not wrong....

You're wrong.  zsh is a highly capable shell, but there are a myriad of
interesting features out there in other shells.  rc has generalised
file-descriptor plumbing; ksh has named background pools, namespaced
variables, generic character class types for matching (think "\d" in a
glob to match a digit), seeking to an offset in a file in a <redirection
with <#((expr)) and also >#((expr)), and seeking forward automatically
to a pattern with <#pattern.  ksh has discipline functions and probably
more I've missed.

Innovation doesn't only happen in zsh and sometimes we too blatantly
copy from others.

Hrm, anyone know why ksh opens the <>filename on stdout while zsh opens
it on stdin?

-Phil


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

* Re: is variable with variable name possible?
  2011-04-04 20:50           ` Stephane Chazelas
  2011-04-04 22:32             ` Mikael Magnusson
  2011-04-05  2:54             ` Aaron Davies
@ 2011-04-05  4:04             ` ZyX
  2011-04-05  4:10             ` ZyX
  3 siblings, 0 replies; 18+ messages in thread
From: ZyX @ 2011-04-05  4:04 UTC (permalink / raw)
  To: Mikael Magnusson, Zsh Users

[-- Attachment #1: Type: Text/Plain, Size: 2230 bytes --]

Reply to message «Re: is variable with variable name possible?», 
sent 00:50:02 05 April 2011, Tuesday
by Stephane Chazelas:

> In your example, if we have to accomodate users enabling
> shwordsplit or globsubst (or running zsh in sh or ksh
> emulation), we need
> 
> : "${(P)somevar::=$PWD}"
If we are talking about script, then you should use `emulate -L zsh' somewhere 
at the top. This will deal with all possible weird options that are set by user.

> eval "$somevar=\$(pwd)"
> or
> eval "$somevar=\$PWD"
> 
> are POSIX and POSIX shells have globsubst and shwordsplit on.
> The above will work regardless of the status of those options.
Somebody said that $somevar contains nothing harmful?

Original message:
> 2011-04-04 18:34:26 +0200, Mikael Magnusson:
> [...]
> 
> > >> > eval "$somevar=\$(pwd)"
> > >> > or
> > >> > eval "$somevar=\$PWD"
> 
> [...]
> 
> > >> How do you feel about this?
> > >> 
> > >> : ${(P)somevar::=$PWD}
> > > 
> > > [...]
> > > 
> > > Try after
> > > 
> > > sudo ln -s . '/*'
> > > cd '/*/*/*/*/*'
> > > setopt globsubst
> > > 
> > > ;-)
> > > 
> > > (if you're lucky, it won't crash your machine).
> > 
> > Pretty sure globsubst breaks your idea too, but fine,
> > 
> > : ${(P)~~somevar::=$PWD}
> > 
> > In fact, globsubst pretty much breaks every hook function i have :P.
> 
> [...]
> 
> eval "$somevar=\$(pwd)"
> or
> eval "$somevar=\$PWD"
> 
> are POSIX and POSIX shells have globsubst and shwordsplit on.
> The above will work regardless of the status of those options.
> 
> In POSIX shells, you need to quote variables to prevent
> globsubst or shwordsplit, even in arguments to ":" because
> globsubst can be very very resource intensive.
> 
> You can bring a machine to its knees with
> 
> : ${var=foo}
> 
> if $var is for instance /*/*/*/../../../*/*/*/../../../*/*/*
> 
> So, you should either use instead:
> 
> var=${var-foo}
> or
> 
> : "${var=foo}"
> 
> In your example, if we have to accomodate users enabling
> shwordsplit or globsubst (or running zsh in sh or ksh
> emulation), we need
> 
> : "${(P)somevar::=$PWD}"
> 
> But
> 
> eval "$somevar=\$PWD"
> 
> is more legible and more portable.

[-- Attachment #2: This is a digitally signed message part. --]
[-- Type: application/pgp-signature, Size: 198 bytes --]

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

* Re: is variable with variable name possible?
  2011-04-04 20:50           ` Stephane Chazelas
                               ` (2 preceding siblings ...)
  2011-04-05  4:04             ` ZyX
@ 2011-04-05  4:10             ` ZyX
  3 siblings, 0 replies; 18+ messages in thread
From: ZyX @ 2011-04-05  4:10 UTC (permalink / raw)
  To: Mikael Magnusson, Zsh Users

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

Reply to message «Re: is variable with variable name possible?», 
sent 00:50:02 05 April 2011, Tuesday
by Stephane Chazelas:

> In your example, if we have to accomodate users enabling
> shwordsplit or globsubst (or running zsh in sh or ksh
> emulation), we need
> 
> : "${(P)somevar::=$PWD}"
If we are talking about script, then you should use `emulate -L zsh' somewhere 
at the top. This will deal with all possible weird options that are set by user.

> eval "$somevar=\$(pwd)"
> or
> eval "$somevar=\$PWD"
> 
> are POSIX and POSIX shells have globsubst and shwordsplit on.
> The above will work regardless of the status of those options.
Somebody said that $somevar contains nothing harmful?

Original message:
> 2011-04-04 18:34:26 +0200, Mikael Magnusson:
> [...]
> 
> > >> > eval "$somevar=\$(pwd)"
> > >> > or
> > >> > eval "$somevar=\$PWD"
> 
> [...]
> 
> > >> How do you feel about this?
> > >> 
> > >> : ${(P)somevar::=$PWD}
> > > 
> > > [...]
> > > 
> > > Try after
> > > 
> > > sudo ln -s . '/*'
> > > cd '/*/*/*/*/*'
> > > setopt globsubst
> > > 
> > > ;-)
> > > 
> > > (if you're lucky, it won't crash your machine).
> > 
> > Pretty sure globsubst breaks your idea too, but fine,
> > 
> > : ${(P)~~somevar::=$PWD}
> > 
> > In fact, globsubst pretty much breaks every hook function i have :P.
> 
> [...]
> 
> eval "$somevar=\$(pwd)"
> or
> eval "$somevar=\$PWD"
> 
> are POSIX and POSIX shells have globsubst and shwordsplit on.
> The above will work regardless of the status of those options.
> 
> In POSIX shells, you need to quote variables to prevent
> globsubst or shwordsplit, even in arguments to ":" because
> globsubst can be very very resource intensive.
> 
> You can bring a machine to its knees with
> 
> : ${var=foo}
> 
> if $var is for instance /*/*/*/../../../*/*/*/../../../*/*/*
> 
> So, you should either use instead:
> 
> var=${var-foo}
> or
> 
> : "${var=foo}"
> 
> In your example, if we have to accomodate users enabling
> shwordsplit or globsubst (or running zsh in sh or ksh
> emulation), we need
> 
> : "${(P)somevar::=$PWD}"
> 
> But
> 
> eval "$somevar=\$PWD"
> 
> is more legible and more portable.

[-- Attachment #2: This is a digitally signed message part. --]
[-- Type: application/pgp-signature, Size: 198 bytes --]

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

* Re: is variable with variable name possible?
  2011-04-04 14:26     ` Bart Schaefer
@ 2011-04-05  8:31       ` Bart Schaefer
  0 siblings, 0 replies; 18+ messages in thread
From: Bart Schaefer @ 2011-04-05  8:31 UTC (permalink / raw)
  To: Zsh Users

The reply quoted below got hung up on my server for several hours
because a CentOS upgrade clobbered my sendmail configuration, which I
didn't notice immediately.  Restored from backup (which is why I'm up
at 1:30am) and it finally went out.  In the interim the discussion has
pointed out an omission I made.

On Mon, Apr 4, 2011 at 7:26 AM, Bart Schaefer <schaefer@brasslantern.com> wrote:
>
> You can also use ${somevar::=$(pwd)} anywhere that you'd reference the
> value of $somevar, including the no-op command:
>
>    : ${somevar::=$(pwd)}
>
>    : ${(As:/:)somevar::=$(pwd)}

The bit I forgot is that to get the effect desired in the original
message, you must add the (P) flag to each of the above, as in:

: ${(P)somevar::=$(pwd)}

: ${(PAs:/:)somevar::=$(pwd)}

As long as I'm expostulating I'll add that "somevar" can even be a
positional parameter, both with and without the (P) flag.  E.g.

: ${(As:/:)1::=$(pwd)}

will splice the current directory hierarchy into the front of "$@".


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

* Re: is variable with variable name possible?
  2011-04-04 22:32             ` Mikael Magnusson
@ 2011-04-05 18:39               ` Stephane Chazelas
  0 siblings, 0 replies; 18+ messages in thread
From: Stephane Chazelas @ 2011-04-05 18:39 UTC (permalink / raw)
  To: Mikael Magnusson; +Cc: Zsh Users

2011-04-05 00:32:27 +0200, Mikael Magnusson:
[...]
> > eval "$somevar=\$PWD"
> >
> > is more legible and more portable.
> 
> But your quotes do nothing (assuming $somevar has a sane value), did you mean
> eval $somevar=\"\$PWD\"
> or possibly more readable version
> eval $somevar='"$PWD"'

Those double quotes are not necessary.

> ? AFAICT, it works without any quotes with globsubst and shwordsplit
> set too though, as long as you quote the $ in $PWD, tried in both zsh
> and bash.
[...]

If you don't quote $somevar, you ask the shell to split it which
it will do according to the current value of IFS (assuming
shwordsplit).

If you quote it, you don't have to worry what the value of $IFS
is.

-- 
Stephane


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

end of thread, other threads:[~2011-04-05 18:45 UTC | newest]

Thread overview: 18+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-04-04 13:37 is variable with variable name possible? zzapper
2011-04-04 13:48 ` Jérémie Roquet
2011-04-04 13:51   ` Jérémie Roquet
2011-04-04 14:13     ` zzapper
2011-04-04 14:26     ` Bart Schaefer
2011-04-05  8:31       ` Bart Schaefer
2011-04-04 14:06   ` Stephane Chazelas
2011-04-04 14:25     ` Mikael Magnusson
2011-04-04 16:28       ` Stephane Chazelas
2011-04-04 16:34         ` Mikael Magnusson
2011-04-04 20:50           ` Stephane Chazelas
2011-04-04 22:32             ` Mikael Magnusson
2011-04-05 18:39               ` Stephane Chazelas
2011-04-05  2:54             ` Aaron Davies
2011-04-05  3:54               ` Phil Pennock
2011-04-05  4:04             ` ZyX
2011-04-05  4:10             ` ZyX
2011-04-04 14:33     ` Jérémie Roquet

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).