mora mora - 4 months ago 11
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

You can't do

A B C ...=$(echo_multiple_values)

but you may wish to do something like below :

myfun(){
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
1
$ echo $c
3
$ echo $z
26