Logo Questions Linux Laravel Mysql Ubuntu Git Menu

How do I properly quote this bash pipeline for watch?





I've built up this pipeline:

echo "scale=2;$(cat io | grep wchar | awk '{print $2}')/(1024^3)" | bc

Now I'm trying to watch it. My knowledge of Bash is really ad-hoc, and so I'm not having success. Tried things like:

watch echo "scale=2;$(cat io | grep wchar | awk '{print $2}')/(1024^3)" | bc # I understand why this fails

watch 'echo "scale=2;$(cat io | grep wchar | awk '{print $2}')/(1024^3)" | bc' # Not enough bash understanding to understand why this fails

What am I doing wrong?


Sample output from cat io is

rchar: 36713294562
wchar: 36788363400
syscr: 27050
syscw: 2314540
read_bytes: 36709928960
write_bytes: 0
cancelled_write_bytes: 0
like image 570
Dmitry Minkovsky Avatar asked Mar 25 '23 01:03

Dmitry Minkovsky

2 Answers

The problem is about the single quotes for awk, you could fix it by escaping single quotes.

watch 'echo "scale=2;$(cat io | grep wchar | awk '"'"'{print $2}'"'"')/(1024^3)" | bc'

It is all about how to escaping single quotes inside single quotes, there is a good explanation "BASH, escaping single-quotes inside of single-quoted strings"

like image 102
Alper Avatar answered Apr 06 '23 00:04


Try having watch invoke the shell:

watch sh -c 'echo "scale=2;$(awk '/wchar/ {print $2}' io)/(1024^3)" | bc'

This is similar to having it invoke a script but without needing a separate file.

like image 38
jilles Avatar answered Apr 06 '23 00:04