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
Best Answer
Replace
with:
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.