0

I need to extract sub-strings and set them as new strings without using echo.

My original variable is delimited by commas, so I have that going for me, but I can't use echo so I don't think I can use cut.

I think I need to use the method shown here but I'm not quite getting it

myvar="1,2,3,4,5,6"

I think it's something along the lines of:

one=${myvar%%,}
two=${myvar}
leiyc
  • 903
  • 11
  • 23
Jake Myers
  • 13
  • 3

2 Answers2

1

Just to leave an answer here:

IFS=',' read a b c d <<< '1,2,3,4'
echo $a $b $c $d
1 2 3 4

For a more detailed explanation, check how to split a list by comma.

accdias
  • 5,160
  • 3
  • 19
  • 31
1

You can just use the slicing from the method you linked:

myvar="1,2,3,4,5"
one=${myvar::1}
two=${myvar:2:1}
three=${myvar:4:1}

Or if you aren't sure of the indexes:

# grab the first one
one=${myvar%%,*}
rest=${myvar#*,}
two=${rest%%,*}
# and so on