3

I'm trying to print out the users who are currently logged in (each logged in user of each terminal). This is what I currently have:

NOWDATE=$(date +"%Y-%m-%d")
NOWTIME=$(date +"%T")
USERS=$(who | awk -v nd="$NOWDATE"_"$NOWTIME" '{printf nd "\n" " User: "$1" Terminal: "$2" Login: "$3" "$4 "\n"}')

Unfortunately, \n does not work, which means that I can't get a newline after the variable $4. The output doesn't do any new lines. How can I achieve that printf does a new line after the variable $4?

Note: I just perform an echo to list the users, so echo $USERS

4

Replace

echo $USERS

with:

echo "$USERS"

On unquoted variables, such as in echo $USERS, the shell performs word splitting. That means that all white space characters, such as newline, are replaced by the first character in IFS (by default: a space). If you want to keep your newlines, use double-quotes.

  • 1
    Thanks! I'm new to linux, so I didn't know that. Perfect answer, it's working fine now :) – Rudi Nov 23 '15 at 20:15

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.