help-bash
[Top][All Lists]
Advanced

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

Re: Splitting variable into two numbers


From: Pascal
Subject: Re: Splitting variable into two numbers
Date: Wed, 21 Jul 2021 10:09:22 +0200

var="{5,8}"
# remove {}
var=${var:1:-1}
# get p
p=${var%%,*}
# get q
q=${var##*,}

Le mar. 20 juil. 2021 à 22:16, <lisa-asket@perso.be> a écrit :

> I could use
>
>
>
> -H n:m
>
>
>
> or
>
>
>
> -H n..m
>
>
>
> From: Greg Wooledge <greg@wooledge.org>
> To: help-bash@gnu.org
> Subject: Re: Splitting variable into two numbers
> Date: 20/07/2021 21:53:53 Europe/Paris
>
> On Tue, Jul 20, 2021 at 03:37:32PM -0400, Chris F.A. Johnson wrote:
> > var="{5,8}"
> > IFS=, read p q <<< "${var//[\{\}]/}"
> >
> > echo "p=$p"
> > echo "q=$q"
>
> The mixing of parameter expansions (to nuke the curly braces) with
> IFS-based splitting is kinda weird. I'd suggest picking one way and
> using it for both steps. Either use parameter expansions exclusively
> (as my earlier suggestion does), or use IFS-based splitting exclusively:
>
> IFS='{,}' read -r _ p q _ <<< "$var"
>
> I still prefer parameter expansions over here-string splitting for this
> case, because the here-string has significantly higher overhead (it
> creates a temporary file).
>
> However, I *would* switch to here-string splitting if there were an
> unknown number of elements in the input list. Then, it's justified.
>
> (This gets us back to "where is this input coming from and why is it in
> this form".)
>
>
>


reply via email to

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