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?

5 Answers 5

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

Simplest solution, no pipe used:

printf '%s\n' $(users)

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.