Bash Scripting on Linux: How to query your Login Name?
In Bash, you have two variables, $USER and $LOGNAME.
$LOGNAME
$LOGNAME provides the primary login name of the user. This name is typically stored in the /etc/passwd file and is read by the login program during the initial session setup. It is an environment variable.
$USER
$USER provides the name of the current user. It is often set by the shell itself or the login manager. It is an environment variable.
What is an environment variable?
An environment variable is a user-definable value that can affect the way running processes will behave on a computer. Environment variables are part of the environment in which a process runs.
From Wikipedia
The script
#!/bin/bash
# User input: Requesting the name
read -p "What is your login name? " name
if [ "$name" = "$LOGNAME" ]; then
echo "This is correct."
else
echo "This is incorrect."
fi
echo "The script terminates."
A small Reel from my Facebook Fanpage for my company „OpenSource Traveler,“ founded on October 6, 2025. I am self-employed and offer Linux courses covering these types of topics.