0

I use watch to monitor the progress of file conversions.

watch -n 2 "echo Converted: $(ls *.mp3 | wc -l) of $(ls *.wav | wc -l) files"

When using command substitutions using the $(command) syntax the values don't get updated each time watch reruns the command within the double quotes. How to do this properly? Since this is a simple script with all kinds of "progress" monitors I'd like to keep the watch command and avoid something like pv.

ilkkachu
  • 133,243
  • 15
  • 236
  • 397
Moka
  • 155
  • 1
  • 9
  • 1
    Related, if not even a duplicate: [What is the difference between the “…”, '…', $'…', and $“…” quotes in the shell?](https://unix.stackexchange.com/q/503013/170373) (`$(...)` is called a command substitution, btw.) – ilkkachu Aug 19 '21 at 20:37

1 Answers1

2

TL;DR: you need to use singlequotes there, like this:

watch -n 2 'echo Converted: $(ls *.mp3 | wc -l) of $(ls *.wav | wc -l) files'

Explanation

Doublequotes are telling Bash to do string interpolation before passing the command to watch, so Bash evaluates those subshells, interpolates the output, and passes the whole shebang to watch, which never evaluates them again because it doesn't know about them.

James S.
  • 183
  • 6