Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Here is what I tried doing:

res=`df | awk 'FNR == 2 { print $5 }'`

Here is my error:

-bash: 53%: syntax error: operand expected (error token is "%")

I Googled 'string to int bash' which showed me $((res+0)) but it doesn't work due to the % in the string (53%). How do I convert it in such a way I can do an if statement such as the one below (but that works)?

if [ "$res2" -ge 50 ]
    echo "Your disk >=50% full!"
share|improve this question
up vote 2 down vote accepted

Let awk do it:

df | awk 'NR == 2 { print $5+0; exit }'
share|improve this answer
Nice! Like bash, awk also stores even numerical values as strings. But this works because when a variable is used in a numerical context, awk strips off any trailing non-numerical junk. (It would also strip off any leading spaces, if $5 happened to contain some. When you use the default field-separation, though, $5 won't.) – dubiousjim Sep 24 '12 at 6:59

Just tell awk to suppress the %:

res=`df | awk 'FNR == 2 { sub("%","",$5); print $5 }'`

But I don't see that the res2 line does anything. Unless you have declare -i res2, bash is still going to treat it as a string. You can do the following just fine:

if [ $res -ge 50 ]; then
    echo "Your disk >= 50% full!"
share|improve this answer

A little variable manipulation would also do:

res=`df | awk 'FNR == 2 { print $5 }'`

if ((  ${res%\%} > limit )); then
    echo "Your disk >= ${limit}% full"

${res%\%} will remove the last occurrence of % from res, (the first % means remove last occurrence and then you need to escape \% to say that's the pattern to be removed)

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.