+91-7678211866  info@peppertechsolutions.com

70+ Linux Scripting Interview Questions & Answers

Interview Preparation

70+ Linux Scripting Interview Questions & Answers (2026)

The complete Linux scripting interview guide covering bash fundamentals, variables, conditionals, loops, functions, text processing, regular expressions, and real-world DevOps scenarios. Answers for system administrators, DevOps engineers, and developer roles.

📅 Updated: June 2026 ⏱ 40 min read 🏷 Linux · Bash · DevOps · Interview · Shell Scripting

🎯 Interview Pro Tip: Linux scripting interviews test practical problem-solving, not memorization. Be ready to write code on a whiteboard or in a text editor. Explain your logic, ask clarifying questions, and think about edge cases. Interviewers often ask “write a script that…” — practice writing code under pressure.

70+ Interview questions with answers
9 Question categories
40 min Complete reading time
40+ Examples Working code samples

Fundamentals & Basics (Q1–Q10)

Q1: What is a shell script and why is it important? Explain the shebang. Foundation question — asked at every level

Answer: A shell script is a text file containing commands that the shell executes sequentially. It automates repetitive tasks, improves efficiency, and ensures consistency across executions.

The shebang (#!): The first line of a shell script. Examples:

  • #!/bin/bash — Use Bash shell (most common)
  • #!/bin/sh — Use POSIX sh (more portable, fewer features)
  • #!/usr/bin/env python3 — Use Python 3 interpreter
  • #!/bin/zsh — Use Zsh shell

Why shebang matters: Without it, the system doesn’t know which interpreter to use. The kernel reads the shebang and executes the appropriate interpreter. Always use #!/bin/bash unless you need POSIX portability.

Q2: Explain the difference between bash and sh. Which should you use? Shell portability and compatibility

Answer:

sh bash
POSIX standard shell (portable across Unix/Linux)Bash extensions (arrays, functions, more features)
Fewer features, lightweightMore powerful, feature-rich
Works on minimal systemsStandard on most Linux distros
Often a symlink to dash or bash in modern systemsFull Bash features available

When to use: Use #!/bin/sh for system startup scripts and scripts that must run on all Unix systems. Use #!/bin/bash for Linux-only scripts where you want arrays, functions, and advanced features. In practice, most modern Linux scripts use Bash.

Q3: What is the exit status ($?) and how do you use it for error handling? Critical for robust scripts

Answer: Every command returns an exit status (exit code) from 0 to 255. $? stores the exit status of the last command.

  • Exit status 0 = Success
  • Exit status 1–255 = Failure (meaning varies by command)
  • $? is set immediately after a command, so capture it right away if needed
#!/bin/bash
cp file.txt /tmp/
if [ $? -eq 0 ]; then
  echo “Copy succeeded”
else
  echo “Copy failed”
  exit 1
fi

Better approach: Use || (or) and && (and) operators: cp file.txt /tmp/ || exit 1

Additional Fundamentals (Q4–Q10 in complete article): Q4: How do you make a script executable? Permissions explanation. • Q5: What are positional parameters ($0, $1, $2, etc.)? • Q6: Explain special variables ($#, $*, $@, $$). • Q7: What is the difference between single and double quotes? • Q8: How do you handle script arguments? • Q9: Explain command substitution with $() and backticks. • Q10: What is set -e and set -x? When to use them?

Variables & Operators (Q11–Q18)

Q11: Explain variable expansion and quoting: single quotes vs double quotes Fundamental concept — causes many bugs if misunderstood

Answer:

Single Quotes (‘…’) Double Quotes (“…”)
Preserves literal strings, no expansionVariables are expanded ($var becomes value)
Cannot include single quotesDouble quotes can be escaped
echo '$VAR' outputs $VAR literallyecho "$VAR" outputs variable value
name=“Alice”
echo ‘Hello $name’ # Output: Hello $name
echo “Hello $name” # Output: Hello Alice

Best practice: Use double quotes for most strings to allow variable expansion. Use single quotes only when you want literal text. Always quote variables: "$var" not $var to prevent word splitting.

Q12: How do you do arithmetic in bash? Explain $(()), expr, and bc Different arithmetic methods with trade-offs

Answer: Three ways to do arithmetic, each with pros/cons:

  • $((expression)) — FASTEST, PREFERRED Built-in Bash arithmetic. No external command. Supports +, -, *, /, %, ++, –. Example: result=$((10 + 5)). Use this 99% of the time.
  • expr — SLOWER External command, slower than $(()). Legacy syntax. Example: result=$(expr 10 + 5). Spawns a process. Avoid unless you need portability to ancient systems.
  • bc — FOR DECIMAL/ADVANCED MATH Floating-point calculator. Example: result=$(echo "10.5 + 3.2" | bc). Use only for decimals or complex math ($(()) handles integers only).
#!/bin/bash
result=$((10 + 5)) # result = 15, FAST
result=$(expr 10 + 5) # result = 15, SLOW
result=$(echo “10.5 + 3.2” | bc) # result = 13.7

Interview answer: “I’d use $(()), which is Bash built-in and fastest. For floating-point math, I’d use bc. I avoid expr because it’s slower and mostly obsolete.”

Additional Variables Questions (Q13–Q18 in full article): Q13: Explain variable scope: local vs global. • Q14: What are environment variables? How do you set them? • Q15: Explain parameter expansion and string manipulation (${var}, ${var:0:5}). • Q16: What is variable substitution? Examples of ${var-default}, ${var:=default}. • Q17: How do you check if a variable is set or empty? • Q18: Explain the difference between let, declare, and typeset.

Conditionals & Control Flow (Q19–Q27)

Q19: Explain test operators: -eq, -lt, -gt, -f, -d, -z, -n. When to use [ ] vs [[ ]] Conditional testing — essential for if statements

Answer: Test operators for conditionals:

  • Integer comparison: -eq (equal), -ne (not equal), -lt (less than), -gt (greater than), -le, -ge
  • String comparison: = (equal), != (not equal), -z (empty string), -n (non-empty)
  • File testing: -f (file exists), -d (directory exists), -e (any file exists), -r (readable), -w (writable), -x (executable), -s (file has size)
  • Logical operators: && (and), || (or), ! (not)
# Using [ ] (POSIX compatible)
if [ “$count” -gt 10 ]; then echo “Greater than 10”; fi
# Using [[ ]] (Bash only, more powerful)
if [[ “$file” == *.txt ]]; then echo “Text file”; fi

[ ] vs [[ ]]: Use [ ] for POSIX portability (sh scripts). Use [[ ]] for Bash (allows wildcards, no quote splitting). [[ ]] is safer for most use cases.

Q20: Write an if-else statement to check if a file exists and is readable Practical conditional logic

Answer:

#!/bin/bash
FILE=“/var/log/syslog”

if [ -f “$FILE” ] && [ -r “$FILE” ]; then
  echo “File exists and is readable”
  wc -l “$FILE”
elif [ -f “$FILE” ]; then
  echo “File exists but not readable (permission denied)”
else
  echo “File does not exist”
  exit 1
fi

Key points: Always quote variables ("$FILE"). Use && for multiple conditions. Use elif for additional checks. Handle all cases (exists, not readable, doesn’t exist).

Additional Conditionals (Q21–Q27 in full article): Q21: Write a case statement example. • Q22: Explain the ternary operator in bash. • Q23: What are && and || operators? Short-circuit evaluation. • Q24: How do you negate a test condition? • Q25: Explain nested if statements and best practices. • Q26: What is the difference between -a and &&? • Q27: How do you test multiple conditions efficiently?

⚡ Full Article Includes Sections Q28–Q70: Loops (for, while, until, break, continue) • Functions (defining, parameters, return values, scope) • File Operations (reading, writing, appending, finding, permissions) • Text Processing (grep, sed, awk with real examples) • Regular Expressions (patterns, character classes, quantifiers) • Advanced Topics (arrays, here documents, error handling, debugging) • Real-World Scenarios (system monitoring, log parsing, deployment automation)

Interview Tips for Linux Scripting Roles

✅ What Interviewers Look For
  • Practical problem-solving: They’ll ask you to write code. Write clean, readable scripts with comments. Test edge cases (empty input, special characters, missing files).
  • Error handling: Show you check exit codes and handle errors gracefully. Use set -e or || exit 1 to fail fast.
  • Best practices: Quote variables, use meaningful names, avoid hardcoding, add logging. These show maturity.
  • Performance awareness: Avoid unnecessary loops, use built-ins over external commands, explain trade-offs (speed vs readability).
  • Real-world experience: Tell stories about scripts you’ve written. Backup automation? Log monitoring? Deployment? Link your answers to production scenarios.
  • Ask clarifying questions: “Is this for a Linux-only environment or should it be portable?” “What’s the expected file size?” “How should I handle errors?” Real engineers clarify requirements before coding.
❌ Common Interview Mistakes
  • Not testing your code: Write it, run it, show it works. Silent failures are red flags.
  • Ignoring edge cases: What if the file is empty? What if the user passes no arguments? Handle it.
  • Hardcoding paths/values: Use variables and configuration files. Shows thinking about reusability.
  • Using obsolete syntax: Backticks instead of $(), expr instead of $(()), [ ] when [[ ]] is better. Interviewers notice.
  • Not explaining your logic: Code silently is boring. Talk through it: “I’m checking if the file exists, then verifying it’s readable, then processing it.”
  • Claiming expertise you don’t have: It’s fine to say “I haven’t used awk much, but I understand the concept.” Interviewers respect honesty.

🎯 Practice Coding Challenge

Write a script that: Takes a log file as argument. Counts ERROR and WARN messages. Displays top 5 most frequent errors. Alerts if error count exceeds 50. Archives the log if it’s older than 7 days. Handles missing files gracefully. Think about: error handling, file operations, text processing (grep, awk), conditional logic, and edge cases. This covers ~70% of interview topics in one challenge.

Land Your Linux/DevOps Role

Master Linux Scripting for Interviews & Production

PepperTech’s hands-on Linux scripting training covers all 70+ interview questions with real-world DevOps automation projects. Learn from certified Linux system administrators with 15+ years of production experience. Build scripts that companies actually use.

✅ 70+ Interview Questions Covered
✅ Live Coding Challenges
✅ Interview Practice & Feedback
✅ Real Linux Environment

📞 Call / WhatsApp +91-7678211866
📧 Email info@peppertechsolutions.com
#LinuxScripting #BashInterviewQuestions #ShellScripting #DevOpsInterview #SystemAdminJobs #Linux #CareerDevelopment #Automation

Comments are closed