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

708 Views Asked by At

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.

1

There are 1 best solutions below

0
On BEST ANSWER

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;
)