zsh-users
 help / color / mirror / code / Atom feed
* Is quoting of the assigned value needed?
@ 2019-11-22  2:03 ` Sebastian Gniazdowski
  2019-11-22 10:45   ` Peter Stephenson
  0 siblings, 1 reply; 9+ messages in thread
From: Sebastian Gniazdowski @ 2019-11-22  2:03 UTC (permalink / raw)
  To: Zsh Users

Hello,
SH_WORD_SPLIT doesn't seem to cause any effects in this context:

setopt SH_WORD_SPLIT
var1="a   b"
var2=$var1
print "$var2"

Output: a   b

And also ${:-} doesn't reveal any effects:

setopt SH_WORD_SPLIT
var1="a   b"
var2=${:-$var1}
print  "$var2"

Output: a   b

Is there however maybe a different option or substitution that would
influence the assignment when unquoted?

-- 
Sebastian Gniazdowski
News: https://twitter.com/ZdharmaI
IRC: https://kiwiirc.com/client/chat.freenode.net:+6697/#zplugin
Blog: http://zdharma.org

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

* Re: Is quoting of the assigned value needed?
  2019-11-22  2:03 ` Is quoting of the assigned value needed? Sebastian Gniazdowski
@ 2019-11-22 10:45   ` Peter Stephenson
  2019-11-22 14:07     ` Perry Smith
  2019-11-22 15:01     ` Sebastian Gniazdowski
  0 siblings, 2 replies; 9+ messages in thread
From: Peter Stephenson @ 2019-11-22 10:45 UTC (permalink / raw)
  To: zsh-users

On Fri, 2019-11-22 at 03:03 +0100, Sebastian Gniazdowski wrote:
> Hello,
> SH_WORD_SPLIT doesn't seem to cause any effects in this context:
> 
> setopt SH_WORD_SPLIT
> var1="a   b"
> var2=$var1
> print "$var2"
> 
> Output: a   b

Correct, there's no splitting there: you've got a quoted assignment, a
single word assignment, and quoted output.

What you should be doing depends on what you're trying to do, which you
don't say.  Do you want var2 to be an array of split parts of $var1?  In
that case, do an array assignment.

var2=($var1)

pws


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

* Re: Is quoting of the assigned value needed?
  2019-11-22 10:45   ` Peter Stephenson
@ 2019-11-22 14:07     ` Perry Smith
  2019-11-22 14:31       ` Peter Stephenson
  2019-11-22 15:01     ` Sebastian Gniazdowski
  1 sibling, 1 reply; 9+ messages in thread
From: Perry Smith @ 2019-11-22 14:07 UTC (permalink / raw)
  To: Peter Stephenson; +Cc: zsh-users

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



> On Nov 22, 2019, at 4:45 AM, Peter Stephenson <p.stephenson@samsung.com> wrote:
> 
> On Fri, 2019-11-22 at 03:03 +0100, Sebastian Gniazdowski wrote:
>> Hello,
>> SH_WORD_SPLIT doesn't seem to cause any effects in this context:
>>  
>> setopt SH_WORD_SPLIT
>> var1="a   b"
>> var2=$var1
>> print "$var2"
>>  
>> Output: a   b
> 
> Correct, there's no splitting there: you've got a quoted assignment, a
> single word assignment, and quoted output.
> 
> What you should be doing depends on what you're trying to do, which you
> don't say.  Do you want var2 to be an array of split parts of $var1?  In
> that case, do an array assignment.
> 
> var2=($var1)

One of the things I do when I’m exploring things like this is write a
silly script that echos out the number of args being passed to it:

% cat <<'EOF' > /tmp/count.sh
#!/bin/zsh -f

echo "$#"
EOF

% chmod +x /tmp/count.sh

% var1="a b"

% setopt nosh_word_split
% /tmp/count.sh $var1   
1

% setopt sh_word_split 
% /tmp/count.sh $var1 
2

Also, when I first started exploring zsh, someone suggested to look at
the zshall man page which is what I do in questions like this.  Then
search for SH_WORD_SPLIT and see all the places that it comes up.

It surprised me that this:

var2=$var1

worked as it does but bash does the same thing.

Drifting off topic slightly... I've had to break some of my old
habits.  With bash, I would just ALWAYS do "${foo}" ... 100% of the
time.  Which is why the assignment above surprised me.  I would have
always just done:

var2="${var1}"

With zsh, not only is this unnecessary ... but it is also "wrong" (or
perhaps I should say "not what I want") when dealing with arrays.  And
using arrays in zsh appears to be where a lot of the power is at which
I never really took advantage of with bash.  Arrays in bash always
seemed like they were playing catch up with ksh and not really a
native bash ground up design and implementation.


[-- Attachment #2: smime.p7s --]
[-- Type: application/pkcs7-signature, Size: 2966 bytes --]

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

* Re: Is quoting of the assigned value needed?
  2019-11-22 14:07     ` Perry Smith
@ 2019-11-22 14:31       ` Peter Stephenson
  2019-11-22 14:59         ` Perry Smith
  0 siblings, 1 reply; 9+ messages in thread
From: Peter Stephenson @ 2019-11-22 14:31 UTC (permalink / raw)
  To: zsh-users

On Fri, 2019-11-22 at 08:07 -0600, Perry Smith wrote:
> It surprised me that this:
> 
> var2=$var1
> 
> worked as it does but bash does the same thing.

It's much clearer like this as you can instantly see if you're doing an
array assignment or not.

If you like unclarity, the original zsh behaviour (changed in very
early days) is still available as an option...


       GLOB_ASSIGN <C>
If  this  option  is  set, filename generation (globbing) is per‐
formed on the right hand side of scalar parameter assignments  of
the  form  `name=pattern  (e.g. `foo=*').  If the result has more
than one word the parameter will become an array with those words
as arguments. This option is provided for backwards compatibility
only: globbing is always performed on  the  right  hand  side  of
array assignments of the form `name=(value)' (e.g. `foo=(*)') and
this form is recommended for clarity; with this option set, it is
not  possible to predict whether the result will be an array or a
scalar.


What you appear to be suggesting is unsurprising (I think, I'm guessing
somewhat) is even less clear: look at the details of what's in $var1 and
decide based on that if you need an array.  Obviously it can't *always*
assign an array as there would be no way of assigning scalar values.

pws

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

* Re: Is quoting of the assigned value needed?
  2019-11-22 14:31       ` Peter Stephenson
@ 2019-11-22 14:59         ` Perry Smith
  0 siblings, 0 replies; 9+ messages in thread
From: Perry Smith @ 2019-11-22 14:59 UTC (permalink / raw)
  To: Peter Stephenson; +Cc: zsh-users

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



> On Nov 22, 2019, at 8:31 AM, Peter Stephenson <p.stephenson@samsung.com> wrote:
> 
> On Fri, 2019-11-22 at 08:07 -0600, Perry Smith wrote:
>> It surprised me that this:
>>  
>> var2=$var1
>>  
>> worked as it does but bash does the same thing.
> 
> It's much clearer like this as you can instantly see if you're doing an
> array assignment or not.

I totally agree.  I never liked the “${foo}” gymnastics.

So far, the defaults of zsh are what I want.  I’m still only in my
first month of using zsh but I’m finding so many things that
solve real world problems that I’ve bumped into.


[-- Attachment #2: smime.p7s --]
[-- Type: application/pkcs7-signature, Size: 2966 bytes --]

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

* Re: Is quoting of the assigned value needed?
  2019-11-22 10:45   ` Peter Stephenson
  2019-11-22 14:07     ` Perry Smith
@ 2019-11-22 15:01     ` Sebastian Gniazdowski
  2019-11-22 15:06       ` Peter Stephenson
  1 sibling, 1 reply; 9+ messages in thread
From: Sebastian Gniazdowski @ 2019-11-22 15:01 UTC (permalink / raw)
  To: Peter Stephenson; +Cc: Zsh Users

On Fri, 22 Nov 2019 at 11:46, Peter Stephenson <p.stephenson@samsung.com> wrote:
>
> On Fri, 2019-11-22 at 03:03 +0100, Sebastian Gniazdowski wrote:
> > Hello,
> > SH_WORD_SPLIT doesn't seem to cause any effects in this context:
> >
> > setopt SH_WORD_SPLIT
> > var1="a   b"
> > var2=$var1
> > print "$var2"
> >
> > Output: a   b
>
> Correct, there's no splitting there: you've got a quoted assignment, a
> single word assignment, and quoted output.
>
> What you should be doing depends on what you're trying to do, which you
> don't say.

I'm thinking on simplifying the plugin standard's proposed $0
handling, which is currently:

0="${${ZERO:-${0:#$ZSH_ARGZERO}}:-${(%):-%N}}"
0="${${(M)0:#/*}:-$PWD/$0}"

I.e.: about skipping the quoting. Before I do this is want to be
completely sure that it'll always work.

> Do you want var2 to be an array of split parts of $var1?  In
> that case, do an array assignment.
>
> var2=($var1)
>
> pws
>

-- 
Sebastian Gniazdowski
News: https://twitter.com/ZdharmaI
IRC: https://kiwiirc.com/client/chat.freenode.net:+6697/#zplugin
Blog: http://zdharma.org

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

* Re: Is quoting of the assigned value needed?
  2019-11-22 15:01     ` Sebastian Gniazdowski
@ 2019-11-22 15:06       ` Peter Stephenson
  2019-11-22 16:26         ` Sebastian Gniazdowski
  0 siblings, 1 reply; 9+ messages in thread
From: Peter Stephenson @ 2019-11-22 15:06 UTC (permalink / raw)
  To: Zsh Users

On Fri, 2019-11-22 at 16:01 +0100, Sebastian Gniazdowski wrote:
> I'm thinking on simplifying the plugin standard's proposed $0
> handling, which is currently:
> 
> 0="${${ZERO:-${0:#$ZSH_ARGZERO}}:-${(%):-%N}}"
> 0="${${(M)0:#/*}:-$PWD/$0}"
> 
> I.e.: about skipping the quoting. Before I do this is want to be
> completely sure that it'll always work.

Scalar assignment is always scalar assignment, yes.  That's basically
the meaning of the text I quoted saying why GLOB_ASSIGN got moved out
to an option.

pws


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

* Re: Is quoting of the assigned value needed?
  2019-11-22 15:06       ` Peter Stephenson
@ 2019-11-22 16:26         ` Sebastian Gniazdowski
  2019-11-23 18:01           ` Sebastian Gniazdowski
  0 siblings, 1 reply; 9+ messages in thread
From: Sebastian Gniazdowski @ 2019-11-22 16:26 UTC (permalink / raw)
  To: Peter Stephenson; +Cc: Zsh Users

On Fri, 22 Nov 2019 at 16:08, Peter Stephenson <p.stephenson@samsung.com> wrote:
>
> On Fri, 2019-11-22 at 16:01 +0100, Sebastian Gniazdowski wrote:
> > I'm thinking on simplifying the plugin standard's proposed $0
> > handling, which is currently:
> >
> > 0="${${ZERO:-${0:#$ZSH_ARGZERO}}:-${(%):-%N}}"
> > 0="${${(M)0:#/*}:-$PWD/$0}"
> >
> > I.e.: about skipping the quoting. Before I do this is want to be
> > completely sure that it'll always work.
>
> Scalar assignment is always scalar assignment, yes.  That's basically
> the meaning of the text I quoted saying why GLOB_ASSIGN got moved out
> to an option.

Thanks!

-- 
Sebastian Gniazdowski
News: https://twitter.com/ZdharmaI
IRC: https://kiwiirc.com/client/chat.freenode.net:+6697/#zplugin
Blog: http://zdharma.org

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

* Re: Is quoting of the assigned value needed?
  2019-11-22 16:26         ` Sebastian Gniazdowski
@ 2019-11-23 18:01           ` Sebastian Gniazdowski
  0 siblings, 0 replies; 9+ messages in thread
From: Sebastian Gniazdowski @ 2019-11-23 18:01 UTC (permalink / raw)
  To: Peter Stephenson; +Cc: Zsh Users

On Fri, 22 Nov 2019 at 17:26, Sebastian Gniazdowski
<sgniazdowski@gmail.com> wrote:
>
> On Fri, 22 Nov 2019 at 16:08, Peter Stephenson <p.stephenson@samsung.com> wrote:
> >
> > On Fri, 2019-11-22 at 16:01 +0100, Sebastian Gniazdowski wrote:
> > > I'm thinking on simplifying the plugin standard's proposed $0
> > > handling, which is currently:
> > >
> > > 0="${${ZERO:-${0:#$ZSH_ARGZERO}}:-${(%):-%N}}"
> > > 0="${${(M)0:#/*}:-$PWD/$0}"
> > >
> > > I.e.: about skipping the quoting. Before I do this is want to be
> > > completely sure that it'll always work.
> >
> > Scalar assignment is always scalar assignment, yes.  That's basically
> > the meaning of the text I quoted saying why GLOB_ASSIGN got moved out
> > to an option.

I have realized that the GLOB_ASSIGN option together with GLOB_SUBST
will cause problems

in="/root/my_directory()"
setopt GLOB_ASSIGN GLOB_SUBST
0=$in
zsh: no matches found: /root/my_directory()

So technically it can be even said that yes, there's a reason to quote
the value, because there exists a shell configuration that will cause
problems with unquoted assignments. So I'm in doubt if the change to
the standard should be done, i.e.: if the quoting should be removed.

Are there maybe some other configurations of the shell when similar
problems might pop up? This could help to clear the doubt. The
standard now uses unquoted assignment:

http://zdharma.org/Zsh-100-Commits-Club/Zsh-Plugin-Standard.html#zero-handling
-- 
Sebastian Gniazdowski
News: https://twitter.com/ZdharmaI
IRC: https://kiwiirc.com/client/chat.freenode.net:+6697/#zplugin
Blog: http://zdharma.org

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

end of thread, other threads:[~2019-11-24 22:54 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
     [not found] <CGME20191122020437eucas1p128174332018d20a266f55007fcf271ba@eucas1p1.samsung.com>
2019-11-22  2:03 ` Is quoting of the assigned value needed? Sebastian Gniazdowski
2019-11-22 10:45   ` Peter Stephenson
2019-11-22 14:07     ` Perry Smith
2019-11-22 14:31       ` Peter Stephenson
2019-11-22 14:59         ` Perry Smith
2019-11-22 15:01     ` Sebastian Gniazdowski
2019-11-22 15:06       ` Peter Stephenson
2019-11-22 16:26         ` Sebastian Gniazdowski
2019-11-23 18:01           ` Sebastian Gniazdowski

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