zsh-users
 help / color / mirror / code / Atom feed
* ${var: -1:1} vs. ${var:-1:1}
@ 2012-11-17 21:10 Ray Andrews
  2012-11-17 21:48 ` Moritz Bunkus
  0 siblings, 1 reply; 5+ messages in thread
From: Ray Andrews @ 2012-11-17 21:10 UTC (permalink / raw)
  To: zsh-users

I just wasted an hour trying to extract the last character from a 
string. My problem was that I did: "${var:-1:1}" and didn't put a space 
in front of the minus sign.  Bash has the same problem, so this isn't a 
zsh specific issue, but I'd feel a lot better if someone could explain 
why that space is necessary. All the shells seem to have these little 
gotchas with spaces. Sometimes they're ignored, sometimes mandatory and 
sometimes forbidden. It sure is a PITA.  I can only compare this to C, 
where such problems are nonexistent. If there is some sort of master 
plan governing spacing, I'd sure like to know what it is.

Thanks, sorry to bitch.


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

* Re: ${var: -1:1} vs. ${var:-1:1}
  2012-11-17 21:10 ${var: -1:1} vs. ${var:-1:1} Ray Andrews
@ 2012-11-17 21:48 ` Moritz Bunkus
  2012-11-17 22:56   ` Ray Andrews
  0 siblings, 1 reply; 5+ messages in thread
From: Moritz Bunkus @ 2012-11-17 21:48 UTC (permalink / raw)
  To: Ray Andrews; +Cc: zsh-users

Hey,

if you don't put a space there then the parser thinks that this the
"${variable:-default} syntax which means "expands to the variable's
content if the variable is set and to 'default' if it is unset". Can
be seen easily with:

[0 mosu@sweet-chili ~] echo ${thisvarisnotset:-1:1}
1:1

So the '1:1' is interpreted as the 'default'. You probably never saw
that result because you were trying to extract the last character from
a string, meaning you always used a variable that was set.

Kind regards,
mosu


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

* Re: ${var: -1:1} vs. ${var:-1:1}
  2012-11-17 21:48 ` Moritz Bunkus
@ 2012-11-17 22:56   ` Ray Andrews
  2012-11-18  1:31     ` Bart Schaefer
  0 siblings, 1 reply; 5+ messages in thread
From: Ray Andrews @ 2012-11-17 22:56 UTC (permalink / raw)
  To: zsh-users

On 17/11/12 01:48 PM, Moritz Bunkus wrote:
> Hey,
>
> if you don't put a space there then the parser thinks that this the
> "${variable:-default} syntax which means "expands to the variable's
> content if the variable is set and to 'default' if it is unset". Can
> be seen easily with:
>
> [0 mosu@sweet-chili ~] echo ${thisvarisnotset:-1:1}
> 1:1
>
> So the '1:1' is interpreted as the 'default'. You probably never saw
> that result because you were trying to extract the last character from
> a string, meaning you always used a variable that was set.
>
> Kind regards,
> mosu
>
I see.  But it's still lousy syntax IMHO.


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

* Re: ${var: -1:1} vs. ${var:-1:1}
  2012-11-17 22:56   ` Ray Andrews
@ 2012-11-18  1:31     ` Bart Schaefer
  2012-11-18  2:25       ` Ray Andrews
  0 siblings, 1 reply; 5+ messages in thread
From: Bart Schaefer @ 2012-11-18  1:31 UTC (permalink / raw)
  To: zsh-users

On Nov 17,  2:56pm, Ray Andrews wrote:
} Subject: Re: ${var: -1:1} vs. ${var:-1:1}
}
} > if you don't put a space there then the parser thinks that this the
} > "${variable:-default} syntax which means "expands to the variable's
} > content if the variable is set and to 'default' if it is unset".
} >
} > So the '1:1' is interpreted as the 'default'. You probably never saw
} > that result because you were trying to extract the last character from
} > a string, meaning you always used a variable that was set.
} >
} I see.  But it's still lousy syntax IMHO.

Well, the var:-word syntax long predates :N:M for string slicing.  Zsh
actually can (and always has) used just ${var[-1]} for the last character
of a string, the :N:M syntax was invented by bash and merely adopted by
zsh for compatibility.


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

* Re: ${var: -1:1} vs. ${var:-1:1}
  2012-11-18  1:31     ` Bart Schaefer
@ 2012-11-18  2:25       ` Ray Andrews
  0 siblings, 0 replies; 5+ messages in thread
From: Ray Andrews @ 2012-11-18  2:25 UTC (permalink / raw)
  To: zsh-users

On 17/11/12 05:31 PM, Bart Schaefer wrote:
> On Nov 17,  2:56pm, Ray Andrews wrote:
> } Subject: Re: ${var: -1:1} vs. ${var:-1:1}
> }
> } > if you don't put a space there then the parser thinks that this the
> } > "${variable:-default} syntax which means "expands to the variable's
> } > content if the variable is set and to 'default' if it is unset".
> } >
> } > So the '1:1' is interpreted as the 'default'. You probably never saw
> } > that result because you were trying to extract the last character from
> } > a string, meaning you always used a variable that was set.
> } >
> } I see.  But it's still lousy syntax IMHO.
>
> Well, the var:-word syntax long predates :N:M for string slicing.  Zsh
> actually can (and always has) used just ${var[-1]} for the last character
> of a string, the :N:M syntax was invented by bash and merely adopted by
> zsh for compatibility.
>
Yeah, I guess it's one of those things where it would be different if 
the whole thing could be rethought, but traditions must be 
accommodated.  I guess if you want a blank sheet of paper, that's when 
you start again with a new shell. But thanks for '${var[-1]}' that's a 
better syntax all around.


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

end of thread, other threads:[~2012-11-18  2:25 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2012-11-17 21:10 ${var: -1:1} vs. ${var:-1:1} Ray Andrews
2012-11-17 21:48 ` Moritz Bunkus
2012-11-17 22:56   ` Ray Andrews
2012-11-18  1:31     ` Bart Schaefer
2012-11-18  2:25       ` Ray Andrews

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