Shell Scripting Basics
Write your first bash scripts with variables, conditionals, loops, and functions.
.sh files in your terminal and run with bash script.sh.What You'll Learn
- Creating and running bash scripts
- Variables, user input, and string operations
- if/elif/else conditionals and comparison operators
- for/while loops and reusable functions
Creating a Bash Script
A bash script is just a text file containing commands. The first line should be a shebang that tells the system which interpreter to use:
#!/bin/bash # This is a comment echo "Hello from a script!" date whoami
To run it:
$ chmod +x script.sh # Make it executable (once) $ ./script.sh # Run it! # OR $ bash script.sh # Run without chmod
Variables
name="Alice" # No spaces around =! age=30 echo "Hello, $name! You are $age." echo "Home directory: $HOME" echo "Current date: $(date +%Y-%m-%d)"
⚠️ Common Mistake
Spaces around = in variable assignment. name = "Alice" fails — bash interprets name as a command. Always write name="Alice" with no spaces.
Scripts & Conditionals
Write your first bash script with variables and if/else.
// Shell Scripting — simulated in JavaScript
console.log("=== Your First Bash Script ===");
console.log(`
#!/bin/bash
# File: greet.sh
name="World"
echo "Hello, $name!"
echo "Today is $(date)"
echo "You are $(whoami)"
`);
console.log("$ chmod +x greet.sh (make it executable)");
console.log("$ ./greet.sh (run it!)");
console.log();
console.log("=== Variables ===");
let name = "Developer";
let count = 42;
console.log("name=" + name);
console.log("count=" + count);
console.log("Greeti
...Loops and Functions
For Loops
# Loop over a list for color in red green blue; do echo "Color: $color" done # Loop over files for file in *.txt; do echo "Processing $file" wc -l "$file" done # C-style loop for ((i=1; i<=10; i++)); do echo "Number: $i" done
Functions
greet() {
local name=$1 # $1 = first argument
echo "Hello, $name!"
}
greet "Alice" # Hello, Alice!
greet "Bob" # Hello, Bob!💡 Pro Tip
Always use local for variables inside functions to avoid polluting the global scope. It's the same best practice as const/let in JavaScript.
Loops & Functions
Practice for/while loops and reusable functions.
// Shell Loops & Functions — simulated
console.log("=== For Loop ===");
console.log("Bash: for i in 1 2 3 4 5; do echo $i; done");
for (let i = 1; i <= 5; i++) {
console.log(" Iteration: " + i);
}
console.log();
console.log("=== Loop Over Files ===");
console.log("for file in *.txt; do echo $file; done");
const files = ["notes.txt", "readme.txt", "todo.txt"];
files.forEach(f => console.log(" Processing: " + f));
console.log();
console.log("=== While Loop ===");
let countdown = 5;
console.l
...📋 Quick Reference
| Concept | Syntax |
|---|---|
| Shebang | #!/bin/bash |
| Variable | name="value" |
| If | if [ cond ]; then ... fi |
| For loop | for x in list; do ... done |
| Function | func() { ... } |
| Make executable | chmod +x script.sh |
🎉 Lesson Complete!
You can now automate tasks with bash scripts! Next: managing running processes with ps, kill, and background jobs.
Sign up for free to track which lessons you've completed and get learning reminders.