bug-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Fwd: Strange results


From: Kerin Millar
Subject: Re: Fwd: Strange results
Date: Fri, 27 Oct 2023 15:34:16 +0100

On Fri, 27 Oct 2023 19:28:15 +0700
Victor Pasko <victor.pasko@gmail.com> wrote:

> See my comments below inline
> 
> On Fri, Oct 27, 2023 at 2:50 AM Kerin Millar <kfm@plushkava.net> wrote:
> 
> > On Fri, 27 Oct 2023 02:00:01 +0700
> > Victor Pasko <victor.pasko@gmail.com> wrote:
> >
> > > ---------- Forwarded message ---------
> > > From: Victor Pasko <victor.pasko@gmail.com>
> > > Date: Fri, Oct 27, 2023 at 1:57 AM
> > > Subject: Re: Strange results
> > > To: Dennis Williamson <dennistwilliamson@gmail.com>
> > >
> > >
> > >
> > > Also
> > >
> > > echo10 ${ASCII_SET:$((-10)):1}
> >
> > This is the "Substring Expansion" kind of parameter expansion.
> > >
> > > and
> > >
> > > echo11 ${ASCII_SET:-10:1}
> >
> > This is the "Use Default Values" kind of parameter expansion.
> >
> > >
> > > have different behaviour:(
> >
> > Substring expansions already imply a numeric context. A single pair of
> > enclosing brackets is enough to avoid this pitfall.
> >
> > ${ASCII_SET:(-10):1}
> >
> > Another method is to have a leading space.
> >
> > ${ASCII_SET: -10:1}
> >
> 
>  Well, it's kind of a workaround to use brackets or extra space, but how to
> recognize such expectations according to string operation with -10:1 ?

That much is easy to explain.

${ASCII_SET:-
            ^ At this point, it looks like a "Default Value" parameter expansion

Thefore, it will be treated as one. If your intention is to use a negative 
offset with a Substring Expansion then you must write your code in such a way 
that it can be disambiguated by the parser. Here are some other ways of going 
about it.

echo "${ASCII_SET:0-10:1}"
i=-10; echo "${ASCII_SET:i:1}"

Keep in mind that the Shell Command Language specification requires that 
"Default Value" parameter expansion be implemented in the way that it is, and 
that there are countless scripts that depend on the status quo. However, the 
Shell Command Language also doesn't suffer from this ambiguity because it 
doesn't specify any means of performing "Substring Expansion" to begin with 
(it's a bash extension). Since there is no way for bash to otherwise know that 
"-10:1" wasn't intended as the word in ${parameter:-word}, you'll have to 
choose a preferred workaround for negative offsets and live with it, as irksome 
as it may be.

As concerns your other questions, I shall respond to them in a separate message.

-- 
Kerin Millar



reply via email to

[Prev in Thread] Current Thread [Next in Thread]