Linux / Unix Shell Script: Get Current User Name

You can get the current user name in a Linux or Unix shell script by using the whoami command or by accessing the $USER environment variable.

Here’s an example using the whoami command:

#!/bin/bash

current_user=$(whoami)
echo "The current user is: $current_user"

And here’s an example using the $USER environment variable:

#!/bin/bash

current_user=$USER
echo "The current user is: $current_user"

Both of these methods will return the username of the user running the script.

Leave a Comment