How do you write conditional statements in bash?

Why Interviewers Ask This

This tests whether you can apply Linux / Shell Scripting knowledge to real-world scenarios. Interviewers are looking for clarity of thought and evidence that you've encountered this in production code.

Answer

Bash uses if/elif/else/fi for conditionals. Syntax: if [ condition ]; then ... elif [ condition ]; then ... else ... fi. Common test operators: -eq/-ne/-lt/-gt (numeric), ==/!= (string), -z (string empty), -n (string not empty), -f (file exists), -d (directory exists), -r/-w/-x (readable/writable/executable). The [[ ]] construct (bash-specific) is preferred: supports &&/|| directly, pattern matching with =~, and avoids word-splitting issues. Short-circuit: command && echo ok runs second only if first succeeds; command || echo fail runs second only if first fails.

Pro Tip

Demonstrate both theoretical understanding and practical experience. Say what it is, then give an example of how you actually used it in a Linux / Shell Scripting codebase.