Search code examples
bashfor-loopnice

How do I nice a for loop on the bash command line?


Help please. I can't figure out the syntax to nice a for loop on the command line.

This is my best guess:

$ nice -n 17 { for _ in {1..2}; do echo howdy; done; }
bash: syntax error near unexpected token `do'

But obviously, that's not correct.


Solution

  • nice is per process, and can not operate on individual shell statements.

    You can either start a new, nice bash instance:

    nice -n 17 bash -c 'for _ in {1..2}; do echo howdy; done;'
    

    Or fork one and renice that:

    ( 
      renice -n 17 "$BASHPID"
      for _ in {1..2}; do echo howdy; done;
    )