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:
current_user=$(whoami)
echo "The current user is: $current_user"
And here’s an example using the $USER
environment variable:
current_user=$USER
echo "The current user is: $current_user"
Both of these methods will return the username of the user running the script.