My questions is similar to the watch question here but with a twist. I need to use quotes, which seem to be stripped by an aliased watch.

I want run watch on a custom slurm squeue command:

$alias squeue_personal='squeue -o "%.18i %.9P %.8j %.8u %.216t %.10M %.6D %R    %V   %S  %Z"'
$alias watch='watch '        

NOTE: as per the other watch question

But this still doesn't work. Because the aliased watch strips out quotations:

Every 2.0s: squeue -o %.18i %.9P %.8j %.8u %.2t %.10M %.6D %R    %V   %S  %Z                                                                                            Fri Jul  6 12:06:57 2018

squeue: error: Unrecognized option: %.9P
Usage: squeue [-A account] [--clusters names] [-i seconds] [--job jobid]
              [-n name] [-o format] [-p partitions] [--qos qos]
              [--reservation reservation] [--sort fields] [--start]
              [--step step_id] [-t states] [-u user_name] [--usage]
              [-L licenses] [-w nodes] [-ahjlrsv]

If I don't use aliases everything is fine. e.g. the following works:

$watch 'squeue  -o "%.18i %.9P %.8j %.8u %.2t %.10M %.6D %R    %V   %S  %Z"'

I'm sure the solution is some small twist to the other watch question but I don't know what.


watch concatenates its command line arguments, joining them with spaces and passes the result as a string to sh -c. So watch ls -l "foo bar" becomes the same as watch ls -l foo bar, and you get a similar problem with squeue. You have two choices:

  1. Add explicit quotes for the shell that watch starts. As you actually did in the last example. It's just that when your alias expands the double-quotes are not quoted. The outer quotes just quote the alias when it's defined. They don't act on the command line when the alias is expanded.

    So, any of these:

    watch squeue -o '"%.18i %.9P %.8j %.8u %.2t %.10M %.6D %R    %V   %S  %Z"'
    watch squeue -o \"%.18i %.9P %.8j %.8u %.2t %.10M %.6D %R    %V   %S  %Z\"
    alias watch='watch '
    alias sq='squeue -o \"%.18i %.9P %.8j %.8u %.2t %.10M %.6D %R    %V   %S  %Z\"'
    watch sq
    alias sq=\''squeue -o "%.18i %.9P %.8j %.8u %.2t %.10M %.6D %R    %V   %S  %Z"'\'
    watch sq

    NOTE: You can use set -x and run watch >/dev/null to see the command that the shell actually runs, you'll see there if the quotes actually get passed along to watch.

  2. Tell watch to skip the shell, and to run the command directly. That way, the separate command line arguments stay separate. Man page:

    -x, --exec
    Pass command to exec(2) instead of sh -c which reduces the need to use extra quoting to get the desired effect.

    watch -x squeue -o "%.18i %.9P %.8j %.8u %.2t %.10M %.6D %R    %V   %S  %Z"
    alias watchx='watch -x '
    alias sq='squeue -o "%.18i %.9P %.8j %.8u %.216t %.10M %.6D %R    %V   %S  %Z"'
    watchx sq
  • Very nice work! – slm Jul 6 '18 at 15:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.