Bird
0
0

You want to check if the current user is root by comparing the user ID. Which command correctly tests this in a script?

hard📝 Application Q9 of 15
Linux CLI - Users and Groups
You want to check if the current user is root by comparing the user ID. Which command correctly tests this in a script?
A[ "$(id -g)" -eq 0 ] && echo "Root user" || echo "Not root"
B[ "$(whoami)" -eq 0 ] && echo "Root user" || echo "Not root"
C[ "$(id -un)" = "root" ] && echo "Root user" || echo "Not root"
D[ "$(id -u)" -eq 0 ] && echo "Root user" || echo "Not root"
Step-by-Step Solution
Solution:
  1. Step 1: Understand root user ID

    Root user has user ID 0.
  2. Step 2: Check command correctness

    [ "$(id -u)" -eq 0 ] && echo "Root user" || echo "Not root" compares numeric user ID to 0 correctly. [ "$(id -un)" = "root" ] && echo "Root user" || echo "Not root" compares username string but uses wrong operator for numeric test.
  3. Final Answer:

    [ "$(id -u)" -eq 0 ] && echo "Root user" || echo "Not root" -> Option D
  4. Quick Check:

    Use numeric ID 0 to identify root [OK]
Quick Trick: Check id -u equals 0 to detect root user [OK]
Common Mistakes:
  • Using string comparison for numeric ID
  • Checking group ID instead of user ID
  • Using username with numeric test operator

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Linux CLI Quizzes