Shell Script To Display Logged in Users, Your UserName and Date / Time

#!/bin/bash
# Write a shell script called hello which output the following:
# + Your username
# + The time and date
# + Who is logged on
# + also output a line of asterices (*******) after each section
 
# function to display a line of asterices
function line(){
	echo "*************************************************"
}
 
echo "Your username : $(echo $USER)"
line # call function
 
echo "Current date and time : $(date)"
line
 
echo "Currently logged on users:"
who
line
Get the latest tutorials on SysAdmin, Linux/Unix, Open Source, and DevOps topics:
CategoryList of Unix and Linux commands
File Management cat
Firewall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network Utilities dig host ip nmap
OpenVPN CentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Manager apk apt
Processes Management bg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searching grep whereis which
User Information groups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPN CentOS 8 Debian 10 Firewall Ubuntu 20.04
4 comments… add one
  • pilot Aug 12, 2011 @ 6:30

    plz help me out.i want to write a linux shell that counts the number of user accounts both normal and privileged user accounts

  • Bill Conner Jul 26, 2010 @ 4:45

    That seems kind of kludgy. In the first place using “echo” as a variable name isn’t a good idea since it’s also a shell command. Using “line” as a variable has the same problem. You can also break long quoted strings over many lines and just invoke echo once.

    function whoson(){
    echo -n “*************************************************\n
    your username : $USER\n
    Current date and time : `date +”%A %d %B %Y at %H:%M”`\n
    Currently logged on users:\n”
    who
    *************************************************\n”
    }

    Use function name anywhere in a script or call it repeatedly or as a sub-routine by just using the function name, “whoson”. Notice that each line ends with a \n which is correctly displayed as a newline if you use the -n option to echo. All the lines will display correctly if properly quoted. The date format is just friendlier.

    Bill

  • .lee. Nov 20, 2009 @ 14:23

    could update with :

    echo “Last logged users:”
    last
    line

    and more … just need to parse the “last” return command.
    thanks !

  • Neo Kim Sep 21, 2009 @ 3:43

    This shell command is so easy!

Leave a Reply

Your email address will not be published. Required fields are marked *

Use HTML <pre>...</pre>, <code>...</code> and <kbd>...</kbd> for code samples.