I'm trying to split a string with two words delimited by spaces, and this snippet isn't working for me:
$ cat > test.sh
#/bin/bash
NP="3800 480"
IFS=" "
echo $NP
echo $NP | read VAR1 VAR2
echo "Var1 : $VAR1"
echo "Var2 : $VAR2"
exit 0
And invoking it gives me:
$ chmod 755 ./test.sh && ./test.sh
3800 480
Var1 :
Var2 :
Where I was hoping to see:
3800 480
Var1 : 3800
Var2 : 480
How can a split a simple string like this in a bash script?
EDIT: (Answer I used) Thanks to the link provided by jw013, I was able to come up with this solution which worked for bash 2.04:
$ cat > test.sh
#!/bin/bash
NP="3800 480"
read VAR1 VAR2 << EOF
$NP
EOF
echo $VAR2 $VAR1
$./test.sh
480 3800