Tuesday, May 14, 2024
3
rated 0 times [  3] [ 0]  / answers: 1 / hits: 538  / 2 Years ago, sat, july 9, 2022, 8:07:42

When I run df -h | grep sdc all is fine, I see numbers in human-readable format:


/dev/sdc1       954G  889G   65G  94% /media/bohdan/teamdata

When I run sh -c "df -h | grep sdc" all is fine, the result is the same...


When I run watch sh -c "df -h | grep sdc" ...suddenly I'm no longer eligible to seeing human-readable numbers:


/dev/sdc1      1000203520  934440320  65763200  94% /media/bohdan/teamdata

Why?


More From » watch-command

 Answers
4

It's because by default, watch itself wraps your command in a sh -c command. This means that you lose a level of quoting, and your command becomes


sh -c df -h | grep sdc

so that sh -c executes plain df, with -h being passed as a positional parameter to the shell.


You can either add additional quoting:


watch "sh -c 'df -h | grep sdc'"

or tell watch not to wrap the command using -x:



   -x, --exec
command is given to sh -c which means that you may need to use
extra quoting to get the desired effect. This with the --exec
option, which passes the command to exec(2) instead.


or simply run


watch "df -h | grep sdc"

without the (unnecessary) explicit sh -c.


[#2299] Sunday, July 10, 2022, 2 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
bathtusain

Total Points: 380
Total Questions: 124
Total Answers: 111

Location: Trinidad and Tobago
Member since Sat, Apr 9, 2022
2 Years ago
;