I want to extract the fields from a line into variables:
aaa bbb ccc
'aaa' => $a, 'bbb' => $b, 'ccc' => $c. How to do it in bash?
I don't want to pipeline the processing, just need to extract them to variables or array.
You can simply do:
read a b c <<<"aaa bbb ccc"
$ echo "a=[$a] b=[$b] c=[$c]"
a=[aaa] b=[bbb] c=[ccc]
As per bash manual:
Here Strings
A variant of here documents, the format is:
<<<word
The word is expanded and supplied to the command on its standard input.
The simplest is:
read a b c
with I/O redirection from where the line is being read:
while read a b c
do
# Process
done < $some_file
If the data is already in a variable, then you could use:
read a b c < <(echo "$variable")
This uses a Bash-specific feature, process substitution.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With