0

So what's the story???

I tried to parse a string in a script and later access one of the tokens, but I cannot get it right. Please help!

#! /bin/bash                                                                    

oIFS="$IFS"
IFS="."
declare -a fields=($1)
echo fields = $fields[0], $fields[1]
IFS="$oIFS"
unset oIFS

word=$fields[1]
echo word = $word

this is put in a script called ww

When called I get

$ ww 64.6.2.3.5.7
fields = 64[0], 64[1]
weight = $fields[1]

but the answer I want is

$ ww 64.6.2.3.5.7
fields = 64, 6
weight = 6
Biffen
  • 6,249
  • 6
  • 28
  • 36
dr_xemacs
  • 51
  • 4
  • 1
    `fields = ${fields[0]}, ${fields[1]}` – jordanm Oct 22 '20 at 14:35
  • 1
    Does this answer your question? [Split string into an array in Bash](https://stackoverflow.com/questions/10586153/split-string-into-an-array-in-bash) – thanasisp Oct 22 '20 at 16:10
  • See linked post for how to access an array item. Currently you access only the first item. Also, no need to save IFS, set it, restore it later. You can set the IFS only for the command populating the array, see the answer there. – thanasisp Oct 22 '20 at 16:11

0 Answers0