size=`df / | awk '{print $4}'`
The following code works, however it also prints the word 'Available':
Available
2079328628
How can I take only the size into size
variable?
size=`df / | awk '{print $4}'`
The following code works, however it also prints the word 'Available':
Available
2079328628
How can I take only the size into size
variable?
You can skip the first line:
size=$(df / | awk 'NR>1{print $4}')
Here, NR>1
only prints the fourth field value if the line is not the first one.
See an online demo.
Using --output
option in gnu df
you can pick & chose fields that you want in output. So you can use:
df --output=avail / | sed '1d'
OR:
df --output=avail / | tail -n +2
OR:
df --output=avail / | awk 'NR>1'
Using just one sub-shell for df
and parsing with Bash built-in features:
#!/usr/bin/env bash
read -r -d '' _ avail < <(df --output=avail /)
No sub-shell at all version:
#!/usr/bin/env bash
trap 'rm -f "$tmpfile"' EXIT
tmpfile="/tmp/avail_$RANDOM"
df --output=avail / >"$tmpfile"
read -r -d '' _ avail <"$tmpfile"
echo "$avail"
Two more using df
and awk
:
df | awk '{if ($4 != "Available") print $4}'
and
df | grep -v Available| awk '{print $4}'