0

Pretty simple, but I didn't find a question on here that asked this exactly.

I thought this would work but it does not.

echo "Your disk usage is ${df -k -h}."

How do I make the above work?

0

Use $(), not ${}

echo "Your disk usage is $(df -h)."

Note that df -h has multi-line output (one line for the header, and one for each mounted filesystem), so including it in an echo statement looks quite ugly.

You can work around that by being more specific about what you want to display (e.g. only disk usage for the fs that contains your home dir) and extracting only the details you want to print. For example:

printf 'Your disk usage on %s is %s\n' $(df -h ~ | awk 'NR==2 {print $6, $3}') 
  • Thanks! Guess I had my brackets and parenthesis mixed up. – David Prentice Nov 28 '15 at 3:55
  • note that df -k -h has multi-line output (one line for header, and one for each mount), so including it in an echo statement won't look good. – cas Nov 28 '15 at 3:57
  • Is there any way around this? If so can you include it in your answer? – David Prentice Nov 28 '15 at 3:59
  • There's no generic way around it, it depends on what exactly you want to show (e.g. just / or ~) and how many filesystems you have mounted (e.g. df | wc -l on my system is 34 largely because I use zfs and have lots of zfs filesystems on my zpools). To just display df for the fs that contains your home dir, try : echo "Your disk usage is $(df -h ~ | sed 1d)." – cas Nov 28 '15 at 4:08
  • btw, it doesn't make any sense to use both -k and -h options of df. – cas Nov 28 '15 at 4:10
0

in ksh you need:

echo "... ${df -k -h;}"

If you want to show a percent of usage for the filesystem in which the current directory is rooted and if you are using a linux system with an up-to-date util-linux package installed, you should do:

printf "Your disk usage is:\t"
(until findmnt -noTARGET,USE% .;do cd ..; done)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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