4

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

1 Answer 1

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
  • 1
    Thanks! I'm new to linux, so I didn't know that. Perfect answer, it's working fine now :) Commented Nov 23, 2015 at 20:15

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.