4

I want to run watch on a command which uses an optional command line argument, like this:

function queue() {
    watch -n 10 'squeue -p ${1:-default} -o "%.8i" '
} 

but the command-line argument isn't used, i.e. the default is only ever used. I tried escaping the $ as per this answer (e.g. watch -n 10 'squeue -p \${1:-default} -o "%.8i" '), but that didn't work either.

Any help appreciated.

DilithiumMatrix
  • 549
  • 1
  • 4
  • 15

1 Answers1

6

When things are in single quotes no variable expansion happens, try

function queue() {
    watch -n 10 "squeue -p ${1:-default} -o '%.8i'"
} 

so the outer quotes are double, which will then do variable expansion within the string

Eric Renouf
  • 1,704
  • 1
  • 12
  • 20
  • This affords one to make a "watch function" which watches user-made function with argument. I found that it is possible to make a watch function (queue() in this case) which runs watch and user-made function (squeue, in this case) which gets its own argument from watch function (queue()). – Joonho Park Apr 26 '23 at 04:29