11

My nice is from GNU coreutils 9.1. I observed that nice -15 is equivalent to nice -n 15:

     nice               # prints 0 for me, the base niceness is 0
     nice -n  15 nice   # prints 15, this is expected
sudo nice -n -15 nice   # prints -15, this is expected
     nice    -15 nice   # prints 15

-15 is a negative number. Why does it increment the niceness in the last example above? The manual (e.g. in Debian 12) does not explain this.

2
  • "Because it's specified that way" (the nice(2) manual page says. "A higher nice value means a low priority.)". Or is your question "Why was it specified that way?"?
    – U. Windl
    Commented Mar 22 at 13:28
  • 2
    You are answering what you think he asked, not what he actually asked... Commented Mar 23 at 2:44

1 Answer 1

30

The portable syntax requires -n if you want to specify an increment. When in doubt, use nice -n.

In nice -15 the argument is not really a negative number. It is a dash concatenated with a positive number. The leading dash indicates it is an option. Compare this e.g. to kill -15 which is equivalent to kill -s 15. Similarly nice -15 is equivalent to nice -n 15.

kill -15 is not as confusing as nice -15, because you don't expect signal numbers to be negative. In the case of nice you were confused because an adjustment to niceness may be negative and <dash><digit(s)> surely looks like a non-positive number. It's easy to interpret -15 as "minus fifteen" in cases where negative numbers make sense.

Note the number after the dash may be with an explicit sign:

     nice -+15 nice   # prints 15
sudo nice --15 nice   # prints -15

Unfortunately --15 looks kinda like a long option (compare: --help) with a positive number, this may add to the confusion. It is another reason to prefer nice -n.

8
  • 5
    You don't expect signal numbers to be negative but negative pids are interpreted as pgids, so there is confusion here as well. Is kill -15 16 the same as kill -s TERM -- -15 16 (kill pgid 15 and pid 16 with SIGTERM) or is it kill -s 15 16 (kill pid 16 with signal 15) Commented Mar 22 at 8:07
  • 2
    This is a good answer in that it explains the reality of the situation; but personally I would have stopped after the first two paragraphs. To my mind, neither nice nor kill make a whole lot of sense. I think the whole idea of passing positive numbers with leading dashes is highly suspect, and should be entirely re-thought. Commented Mar 23 at 2:51
  • 2
    Something similar happens with head and tail: head -5 and tail -5 mean head -n 5 and tail -n 5, respectively.
    – ruakh
    Commented Mar 23 at 8:16
  • Another victim of using the same ASCII symbol for hyphen and minus. Commented Mar 23 at 21:50
  • @PaulDraper: Would you prefer it if nice ‐15 and nice −15 were distinct commands that behaved completely differently?
    – ruakh
    Commented Mar 24 at 5:20

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .