On Thu, Aug 19, 2021 at 10:47:35AM -0400, Chet Ramey (chet.ramey@case.edu)
wrote:
> On 8/19/21 1:10 AM, Tapani Tarvainen wrote:
>
> > Incidentally, why does ${var:offset} with a negative offset result in
> > an empty string when the variable is shorter than the offset? E.g.,
> >
> > x=abc
> > echo ${x: -6}
> >
> >
> > I find that counterintuitive,
>
> Why? It's an out-of-bounds error.
Maybe my intuition is strange, but when I specify "last six characters"
of a string that's shorter, I find it odd to get an empty string.
I would understand if it actually generated an error (like ${x::-6} does),
but returning an empty string silently is surprising.
In general, when I try to do something that can't be done, the expected
result is either an error message or leaving the input unchanged.
In this particular case it is debatable which would be better, but
I think the latter is likely to useful more often.
> > and it'd actually be useful to
> > be able to get "at most N characters from the end".
>
> If you want that, you can use ${#x} to get the length and work from there.
Sure. And there are other ways, too. None quite as easy though.
--
Tapani Tarvainen