Robert Elder Software Inc.
  • Home
  • Store
  • Blog
  • Contact
  • Home
  • Store
  • Blog
  • Contact
  • #linux
  • |
  • #commandline
  • |
  • #softwareengineering
  • |
  • #embeddedsystems
  • |
  • #compilers
  • ...
  • View All >>

Intro To 'whoami' Command In Linux

2023-05-26 - By Robert Elder

     I use the 'whoami' command to determine who I am.  The 'whoami' command always shows the current user:

whoami
robert

Using 'sudo' With The 'whoami' Command

     If I use the 'sudo' command to become the 'tomcat' user, the 'whoami' command will print 'tomcat':

sudo -u tomcat whoami
tomcat

     The same thing happens if I use 'sudo' to become the 'postgres' user:

sudo -u postgres whoami
postgres

Example Use Of 'whoami' Command

     The 'whoami' command is very useful for writing automated scripts.  For example, in order to run this 'fdisk' command I need to be the root user:

fdisk -l /dev/sda

     I can write a script called 'show-disk.sh' that uses the 'whoami' command to check the current user before trying to run this command:

#!/bin/bash

if [ "$(whoami)" == "root" ]; then
        fdisk -l /dev/sda
else
        echo "Must be root to run this script."
fi

     If the current user is not the root user, then a helpful error message is printed:

./show-disk.sh
Must be root to run this script.

     Otherwise, if the current user is root then the fdisk command is allowed to run:

sudo ./show-disk.sh
Disk /dev/sda: 1.84 TiB, 2000398934016 bytes, 3907029168 sectors
Disk model: Crucial X6 SSD
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 4096 bytes / 33553920 bytes
Disklabel type: gpt
Disk identifier: 098ECDEF-530E-4249-9BCE-3DF21B249458

Device     Start        End    Sectors  Size Type
/dev/sda1   4096 3907029134 3907025039  1.8T Linux filesystem

     And that's why the 'whoami' command is my favourite Linux command.

Intro To 'stty' Command In Linux
Intro To 'stty' Command In Linux
Published 2023-10-04
Terminal Block Mining Simulation Game
$1.00 CAD
Terminal Block Mining Simulation Game
Intro To 'nproc' Command In Linux
Intro To 'nproc' Command In Linux
Published 2023-07-15
Intro To 'comm' Command In Linux
Intro To 'comm' Command In Linux
Published 2023-09-06
How To Force The 'true' Command To Return 'false'
How To Force The 'true' Command To Return 'false'
Published 2023-07-09
A Surprisingly Common Mistake Involving Wildcards & The Find Command
A Surprisingly Common Mistake Involving Wildcards & The Find Command
Published 2020-01-21
A Guide to Recording 660FPS Video On A $6 Raspberry Pi Camera
A Guide to Recording 660FPS Video On A $6 Raspberry Pi Camera
Published 2019-08-01
Intro To 'chroot' Command In Linux
Intro To 'chroot' Command In Linux
Published 2023-06-23
Join My Mailing List
Privacy Policy
Why Bother Subscribing?
  • Free Software/Engineering Content. I publish all of my educational content publicly for free so everybody can make use of it.  Why bother signing up for a paid 'course', when you can just sign up for this email list?
  • Read about cool new products that I'm building. How do I make money? Glad you asked!  You'll get some emails with examples of things that I sell.  You might even get some business ideas of your own :)
  • People actually like this email list. I know that sounds crazy, because who actually subscribes to email lists these days, right?  Well, some do, and if you end up not liking it, I give you permission to unsubscribe and mark it as spam.
© 2025 Robert Elder Software Inc.
SocialSocialSocialSocialSocialSocialSocial
Privacy Policy      Store Policies      Terms of Use