Question

I use brand new usage of 'bash'.

func(){
  echo Dan 38
}
read name age < <(func)
echo "name=$name age=$age"

How to convert these into dash? (In fact it is busybox's shell)

I use following lines to replace read name age < <(func)

func > /tmp/$$
name=`cat /tmp/$$ | awk '{print $1}'`
age=`cat /tmp/$$ | awk '{print $2}'`
rm /tmp/$$

But, I'm wonder is there better solution?

Was it helpful?

Solution

Solution 1, C-style

Modify the function to accept the names (you can think of them as pointers) of the variables where the result is to be written.

func () { eval "$1=Dan $2=38"; }
func name age
echo "name=$name age=$age"

Solution 2, manual split

This is similar to your solution, but has the advantage that it doesn't use files or call to external programs like awk.

func(){ echo Dan 38 }
both=$(func)
name=${both%% *}
age=${both#* }
echo "name=$name age=$age"
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top