A system administrator is writing a shell script that reads usernames from a file named users.txt and needs to verify whether each account exists on the local system. Which command is the most suitable to place inside the loop to test if a specified user account is present?
The id command outputs user and group identity details for the supplied username. When the named user exists, id returns the relevant information and an exit status of 0; if the user does not exist, it produces an error and a non-zero exit status, allowing the script to branch accordingly.
pwd only prints the current working directory, whoami shows the effective user running the script (and accepts no username argument), and hostname displays the machine's host name-none of which can check another user's existence.
Ask Bash
Bash is our AI bot, trained to help you pass your exam. AI Generated Content may display inaccurate information, always double-check anything important.
How does the `id` command check if a user exists?
Open an interactive chat with Bash
What alternatives to `id` can be used to check if a user exists?
Open an interactive chat with Bash
Why can't `pwd`, `whoami`, or `hostname` verify a user account's existence?