Bash supports if, case, and loops (for, while). Tests use [[ ]] (Bash) or [ ] (POSIX test)—prefer [[ ]] in Bash scripts.
if and [[ ]]
score=85
if [[ $score -ge 90 ]]; then
echo "A"
elif [[ $score -ge 80 ]]; then
echo "B"
else
echo "C or below"
fi-ge means greater than or equal (numeric test inside [[ ]]).
for loops
for f in *.txt; do
echo "file: $f"
done
for n in 1 2 3; do
echo "n=$n"
donefor f in *.txt loops over matching files—glob order matters for reproducibility.
while loop
count=0
while [[ $count -lt 3 ]]; do
echo "count=$count"
count=$((count + 1))
done
Important interview questions and answers
- Q: if vs [[ ]]?
A:[[is a Bash keyword with safer parsing than the external[command. - Q: for file in *.log purpose?
A: Runs the loop body once per matching filename.
Self-check
- Which test operator means greater or equal in [[ ]]?
- What keyword closes an if block?
Tip: Prefer [[ ]] in Bash scripts; use [ ] when you must stay POSIX-sh compatible.
Interview prep
- [[ ]] advantage?
Bash keyword with safer parsing than external [ test.
- fi purpose?
Closes an if/elif/else block.