You can get all values in one go
Code:
#!/bin/bash
{
read x
read x memtotal memused memfree memshared membuf memavail x
read x swptotal swpused swpfree x
} < <( free -m )
echo "memused $memused"
Each read command reads one line.
Each field reads into a variable. Extraneous fields are read into the last variable.
The <( ) is a "process substitution"; the { code } runs in the main shell.
In contrast, a pipe:
Code:
free -m |
{
read x
read x memtotal memused memfree memshared membuf memavail x
read x swptotal swpused swpfree x
echo "memused $memused"
}
echo "memused $memused"
Here the { code } runs in a subshell; the variables are not copied back to the main shell.