Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: ${var: -1:1} vs. ${var:-1:1}

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.

Messages sorted by: Reverse Date, Date, Thread, Author