ps aux --sort=-%cpu | grep -v 'whoami'
command is supposed to output processes that are not started by the effective user. However, it prints out my user's processes as well as other users'. Please explain what's wrong.
ps aux --sort=-%cpu | grep -v 'whoami'
command is supposed to output processes that are not started by the effective user. However, it prints out my user's processes as well as other users'. Please explain what's wrong.
grep -v 'whoami'
excludes lines matching literal stringwhoami
If you want to exclude lines matching the output of the
whoami
command, you need to replace the single quotes with backticksor use the
$(...)
form of command substitution insteadAlternatively, you could skip the
grep
altogether by negating the user selection directly:As steeldriver's answer already explained, your command is wrong because it filters against literal string
whoami
, and you could usegrep -v "$(whoami)"
; you could also usegrep -v "$USER"
to achieve desired effect.Another, better way would be to let
ps
handle filtering with-Nu
as steeldriver showed ortop
:However, I would recommend you use actual login name - the literal string - as in
grep -v 'myuser'
for three reasons:It is possible to create a user with
*
character:Why is this important ? Because when you use
$()
without quoting, wildcard can become an issue with shell globbing if there exist files which may contain part of the username, then the command will break:Notice how shell expanded
myuser1*
intomyuser1.pdf
andmyuser1.txt
, in accordance with shell globbing. Not what you expected, right ?Second reason - if you're logging into multiple usernames ( which some system administrators may do) or have multiple terminals open, you can get confused by
whoami
output:If your intent is to filter out
root
processes, this will work, but if you're logged in asroot
yet want to filter out your admin user's processes -whoami
will give you not the thing you intended.Environment variables can be unset:
So what did we learn from this ?
grep