How to print last login time?

11,966

(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
Share:
11,966

Related videos on Youtube

Rui F Ribeiro
Author by

Rui F Ribeiro

Updated on September 18, 2022

Comments

  • Rui F Ribeiro
    Rui F Ribeiro over 1 year

    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?

  • Jeff Schaller
    Jeff Schaller over 5 years
    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.
  • Richard A. Allcorn  - rAllcorn
    Richard A. Allcorn - rAllcorn over 5 years
    I'm not so interested in who gets the credit but in helping out. Thanks for pointing that out though.