0
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?

Gilbert Williams
  • 970
  • 2
  • 10
  • 24

4 Answers4

2

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.

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
2

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'
anubhava
  • 761,203
  • 64
  • 569
  • 643
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"
Léa Gris
  • 17,497
  • 4
  • 32
  • 41
0

Two more using df and awk:

df | awk '{if ($4 != "Available") print $4}'

and

df | grep -v Available| awk '{print $4}'
sinkmanu
  • 1,034
  • 1
  • 12
  • 24