Skip to main content
junior
beginner
Linux

Bash Scripting Basics

Question

What is the shebang line and how do you write a basic bash script?

Answer

The shebang (#!) tells the OS which interpreter to use. #!/bin/bash specifies bash. Basic script structure: shebang, variables (VAR=value), conditionals (if/then/fi), loops (for/while), and functions. Make scripts executable with chmod +x. Use set -e to exit on errors and set -x for debugging.

Why This Matters

Bash scripting is essential for automating repetitive tasks, deployment scripts, system administration, and CI/CD pipelines. Even with modern tools like Ansible or Terraform, bash scripts remain valuable for quick automation and glue code.

Code Examples

Basic script structure

bash

Loops and input handling

bash
Common Mistakes
  • Forgetting to quote variables (causes issues with spaces)
  • Using == instead of = in [ ] conditionals
  • Not handling script failures (use set -e or check $?)
Follow-up Questions
Interviewers often ask these as follow-up questions
  • What's the difference between $* and $@ for script arguments?
  • How do you handle errors gracefully in bash scripts?
  • When would you choose bash over Python for automation?
Tags
bash
scripting
automation
linux
shell