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.

0

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.