-1

Using du command to get disk space, but only wanted the value, without the path. I am able to print using awk, but how to store only the value into variable?

du -sh $path | awk '{print $1}'
27G


du -sh $path
27G     $path
toolic
  • 57,801
  • 17
  • 75
  • 117
walker
  • 157
  • 5

1 Answers1

0

You could use a variable to capture and store the output of du command:

disk_space=$(du -sh $path | awk '{print $1}')
echo "$disk_space"
Gilles Quénot
  • 173,512
  • 41
  • 224
  • 223
ramsay
  • 3,197
  • 2
  • 14
  • 13
  • For robustness, [quote your variables](https://stackoverflow.com/questions/10067266/when-to-wrap-quotes-around-a-shell-variable), though, especially `"$path"` – tripleee May 11 '23 at 03:16