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?
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?
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}')
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)
df -k -h
has multi-line output (one line for header, and one for each mount), so including it in anecho
statement won't look good. – cas Nov 28 '15 at 03:57df | wc -l
on my system is34
largely because I use zfs and have lots of zfs filesystems on my zpools). To just displaydf
for the fs that contains your home dir, try :echo "Your disk usage is $(df -h ~ | sed 1d)."
– cas Nov 28 '15 at 04:08-k
and-h
options ofdf
. – cas Nov 28 '15 at 04:10printf 'Your disk usage on %s is %s\n' $(df -h ~| awk '/\// {print $6, $3}')
– cas Nov 28 '15 at 04:14