mora mora - 1 year ago 64
Bash Question

Can I output multiple value in bash, like read A B C <<< "1 2 3"?

I am learning bash shell.
I found a command "read" which can deliver multiple values to different variables, like

read A B C ... <<< "1 2 3 ..."

Now I make a function

function echo_multiple_values() {
echo "1 2 3 ..."

Do I have a smart way to output multiple values from a function, like

A B C ...=$(echo_multiple_values)

Thank you very much.

Answer Source

You can't do

A B C ...=$(echo_multiple_values)

but you may wish to do something like below :

arr=( {1..3} ) #{a..b} is a bash range
echo "${arr[@]}"

read a b c <<<"$(myfun)"

Also, you could do something like below

$alphabets=( {a..z} )
$ nums=( {1..26} )
$ read "${alphabets[@]}" <<<"${nums[@]}"
$ echo $a
$ echo $c
$ echo $z
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download