I logged into the linux system and try to get all the current users. I used users
command in terminal and the result is displayed as "user1 user2 user3 user4".
Are there any ways to break the username line and make each username occupy one line?
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It only takes a minute to sign up.
Sign up to join this communityI logged into the linux system and try to get all the current users. I used users
command in terminal and the result is displayed as "user1 user2 user3 user4".
Are there any ways to break the username line and make each username occupy one line?
users | tr -s ' ' '\n'
This will take the output of the users
utility and replace all spaces with newlines using tr
, removing multiple consecutive newlines from the result (with -s
).
Pipe that through sort -u
to get unique usernames.
There are probably better ways but:
for user in $(users); do echo "$user"; done
or using the fmt
command:
users | fmt -w 1
I'd go with this, leveraging 'who', awk, sort and uniq.
who | awk '{ print $1 }' | sort | uniq
This makes the assumption that you want unique user names and not each login session.
from https://stackoverflow.com/a/37843747/52074 this solution has 6 upvotes:
users | xargs -n 1