3

I'd like to know how to set in .bashrc file to print the last login time of users when logging into another user in terminal. I do know that with who or last, but I'd like something a little more customizable that can be formatted and only runs once per login, preferably to show in a format like so;

last login time [ 2015-02-13 @ 3:50:00 AM ]
user@host /path/dir/here $

that gets printed to the terminal screen before the bash prompt, and if possible to set and format color on the time and date stamp. How can I achieve this to customizing the bash prompt output?

2 Answers 2

2

(Just as a guideline, the format is not exactly the same):

ll=$(last -1 -R  $USER | head -1 | cut -c 20-)
export PS1="last login time [$ll]"'\n\h:\W\$ '

Edit: if you want last information to be printed only once (wise idea)

ll=$(last -1 -R  $USER | head -1 | cut -c 20-)
echo "last login time [$ll]"    # adjust to your login messages, fortunes, etc
export PS1='\n\h:\W\$ '         # replace by your favorite prompt
2
  • This works okay… but this doesn't just print that one time after logging in via terminal, it appears after each command is finished. Perhaps there is a way to improve on this answer so it only runs once after logging in. Commented Feb 13, 2015 at 8:44
  • There is always space for improvement, that is the goal of SO! (edited) Commented Feb 13, 2015 at 9:16
0

Try putting the following line into your /etc/profile file ...

echo "your last login:  `last -1 -R $USER` "

This will limit the results to 1 line and report the user's last login, during the login process. Afterwards it will not re-occur until the next login.

2
  • I'd recommend the user's personal ~/.bashrc file over the system-wide one, in case other users are not interested in such output. Otherwise, this seems to use the code from JJoao's answer and so should be credited there. Commented Nov 29, 2018 at 19:29
  • I'm not so interested in who gets the credit but in helping out. Thanks for pointing that out though. Commented Dec 5, 2018 at 17:37

You must log in to answer this question.