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: Greg Wooledge
Subject: Re: Splitting variable into two numbers
Date: Tue, 20 Jul 2021 15:53:53 -0400

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]