I am essentially trying to assing certain portions of the output of a shell command to mulitple variables, but I don't know how to do that.For simplicity's sake, assume that the command on the shell, when executes, printsone two three fourwhich can be simulated with aecho one two three four(although the actual comannd is different)Now, I'd like to assign the second and fourth word of the output (in this case two and four) to the variables w1 and w2.I thought I could use the read command like so:echo one two three four | awk '{print $2   $4}' | read w1 w2but this doesn't work, probably because the read command is executed in a sub-process.So, how would I go about to achieve what I am after?
How to assign values to multiple variables in command line with bars
shell;bash
This doesn't work because the read runs in a child process which cannot affect the parent's environment.You have a few options:You can convert your command to:w1=$(echo one two three four | awk '{print $2}')w2=$(echo one two three four | awk '{print $4}')Alternatively, change IFS and use set:OIFS=$IFSIFS=' 'set -- $(echo one two three four | awk '{print $2 $4}')IFS=$OIFSw1=$1 w2=$2or a Here String:read w1 w2 w3 w4 <<< one two three four