Is "ps -u" Really a Bad Syntax?

75

6

IMHO ps -u shows a very useful output, much better than ps -u $USER:

$ ps -u
Warning: bad syntax, perhaps a bogus '-'? See /usr/share/doc/procps-3.2.8/FAQ
USER         PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
elastic   234897  0.0  0.0 105980  1336 pts/2    S+   Oct10   0:00 /bin/bash ./run.sh collector-json-1.conf
elastic   234899 48.7  7.1 10087120 4433104 pts/2 Sl+ Oct10 2804:11 /usr/java/jdk1.7.0_09_x64/bin/java -Xmx6144m -XX:+UseParNewGC -XX:+UseConcMarkSweepGC -Djava.awt.headless=true -XX:CMSInitiatingOccupancyFraction=75 -XX:+UseCMSInitiatin
:

compared to

$ ps -u $USER
    PID TTY          TIME CMD
 234897 pts/2    00:00:00 run.sh
 234899 pts/2    1-22:44:04 java
:
  1. But, why is it "bad syntax"? /usr/share/doc/procps-3.2.8/FAQ doesn't help much.
  2. What would be a "proper syntax" to achieve the exact same output?

In case it's important:

$ uname -a
Linux h22k34.local 2.6.32-042stab044.17 #1 SMP Fri Jan 13 12:53:58 MSK 2012 x86_64 x86_64 x86_64 GNU/Linux

sjngm

Posted 2014-10-14T06:49:07.357

Reputation: 1 873

5And now for the tough part: Which answer should get the checkmark? – sjngm – 2014-10-14T09:48:59.643

Actually I thought I should let you, the users, decide. The one with the higher upvotes should get it. But, you are not making this any easier ;) – sjngm – 2014-10-15T05:26:11.280

6when both answers are equally good, I upvote both, and give the checkmark to the user with the lower rep, especially when the difference is more than 100k. – Frank Thomas – 2014-10-15T11:46:59.590

1@FrankThomas done :) – sjngm – 2014-10-15T12:16:51.680

On SysV-based systems (or ones whose ps comes from that line of things), I frequently use ps -fu $USER... getting ps -f output formatting which is somewhat similar to ps u formatting, yet also getting the specify-a-user thing. In case you wanted both at the same time. – lindes-hw – 2014-10-15T17:48:58.270

Use ps u, or export I_WANT_A_BROKEN_PS=1; ps -u – Paul Draper – 2014-10-18T16:07:15.740

Answers

131

The correct syntax, which returns the same output, would be:

ps u

There is a good reason why modern syntax for ps is a mess. Historically, there were two incompatible version of ps. Options with a leading dash were inherited from the AT&T Unix version of ps. Options without a leading dash were inherited from BSD. The version of ps that Linux distributions generally use is GNU which has merged both sets of options together, as well as added its own set of options that start with a leading double-dash.

Thus, ps u is BSD-style and ps -u $USER is AT&T-style. The fact that GNU ps allows you to run ps -u and, other than the warning, get the same output as ps u shows that GNU is attempting to make the best of a bad situation.

John1024

Posted 2014-10-14T06:49:07.357

Reputation: 13 893

Actually, there is not a good reason for the mess. There are several bad ones, and the purported dichotomy of "GNU" and "BSD" is a fallacy propounded by a Linux manual page. See https://unix.stackexchange.com/a/511530/5132 .

– JdeBP – 2019-04-09T18:24:45.777

84

The ps command historically had wildly different syntax in BSD and System V Unix.

  • In BSD ps, the u option (no dash) takes no parameter and shows the "user-oriented output" with the additional columns.

  • In SunOS ps, the -u option (with dash) takes a username as parameter and only includes processes owned by that user, but without changing the display format.

(As another very common example, BSD e means "show environment", while SunOS -e means "show everyone's processes".)

Linux procps ps tries to support both styles. So if you use the 'dash' option -u, it will expect it to be the SunOS "filter this user" option, not the extended columns option. The two are confused frequently enough, however, that procps tries to Do What You Meant – if the username is missing, it'll assume you gave it a BSD option but used SunOS syntax.

(There were in fact so many different variants of ps that the procps has an actual table of "personalities" to force ambiguous behavior to be interpreted as one style or another or yet another – in addition to knobs like "UNIX95", "CMD_ENV", "_XPG", "I_WANT_A_BROKEN_PS"...)

user1686

Posted 2014-10-14T06:49:07.357

Reputation: 283 655

29"I_WANT_A_BROKEN_PS" hah. – None – 2014-10-14T09:54:04.353

42

...At first, I thought that was a joke. But nooooo...

– Izkata – 2014-10-14T16:07:07.283

9Hmm, as it turns out, this is exactly the option to hide the "ambiguous usage" the warnings that OP is seeing. – user1686 – 2014-10-15T11:11:23.763