0

I swear I came across a way to do this the other day, but don't remember it exactly:

a="blah,test"
echo ${a[,1]}
blah

Basically, it seemed it was setting a delimiter in an array, and specifying what part of the variable to use. Basically the same as this in awk:

echo $a | awk -F "," '{print $1}'

Was I taking crazy pills, or is something like this possible? I need this because I am echoing out text from the variable in the middle of a sentence, and need to choose which part to use.

cashman04
  • 1,134
  • 2
  • 13
  • 27

1 Answers1

4
a="blah,test"
IFS=',' read -a array <<< "$a"
echo "${array[1]}"

IFS is input field separator, read uses it to split the line into words see here. With -a words are assigned to sequential indices of the array. <<< is here strings causes $a to be expanded and supplied to the read command.

a5hk
  • 7,532
  • 3
  • 26
  • 40
  • Try and add more to the answer then just code. Try and explain what the code block is doing, so the poster can learn from it and gain some knowledge. – Walls Apr 25 '14 at 19:26