Shell Scripting Command Guide for Bash, Sh & Zsh
Turning commands into programs.
What Is Shell Scripting, and Why Use It?
A shell script is a text file full of commands — the same ones you type at the prompt — saved with a .sh extension and made executable. When you run it, the shell executes the lines top to bottom. Variables, conditionals, and loops turn those lines from a recording of your typing into an actual program.
Why Learn Shell Scripting?
- Anything you type more than twice belongs in a script. Automation is the whole point of the command line.
- Scripts are the standard glue for servers, cron jobs, backups, and deploy steps.
- The syntax looks intimidating; in practice you need five constructs — variables, conditionals, loops, functions, exit codes — to do almost everything.
A shell script is just a list of commands with superpowers: variables, decisions, and repetition. If you can type it, you can script it.
1. The Shebang Line
The first line tells the system which interpreter to use:
#!/bin/bash # run with bash
#!/bin/sh # run with the POSIX shell
#!/usr/bin/env python3 # the same trick works for any language
chmod +x deploy.sh # make it executable
./deploy.sh # run it
2. Variables
name="Ada" # no spaces around =
count=3
echo "$name has $count files"
echo "${name}!" # braces mark the variable's end
unset name # delete a variable
readonly PI=3.14159 # make it unchangeable
3. Conditionals
if [ -f "/etc/passwd" ]; then
echo "file exists"
elif [ -d "/tmp" ]; then
echo "tmp is a directory"
else
echo "nothing found"
fi
# Common test operators
[ "$a" = "$b" ] # string equality
[ "$a" -eq 3 ] # numeric equality (-ne, -lt, -gt, -le, -ge)
[ -f file ] # is a regular file?
[ -d dir ] # is a directory?
[ -x cmd ] # is it executable?
[ -z "$s" ] # is the string empty?
4. Loops
for file in *.log; do
wc -l "$file"
done
i=1
while [ $i -le 5 ]; do
echo "line $i"
i=$((i + 1))
done
5. Functions and Exit Codes
greet() {
echo "Hello, $1" # $1 is the first argument
}
greet "Ada"
ls /does/not/exist
echo $? # exit code: 0 = success, anything else = failure
Common Pitfalls
| Mistake | Fix |
|---|---|
name = "Ada" fails | No spaces around = |
Script runs but command not found | Check the shebang, or run with bash script.sh |
[ -f $file ] breaks on empty values | Always quote: [ -f "$file" ] |
Missing fi / done | Every if needs fi; every for/while needs done |
Forgetting chmod +x | chmod +x script.sh before ./script.sh |
set -e surprises | It exits on any failed command — great for scripts, confusing at first |
Related Pages
| Page | What It Covers |
|---|---|
| Aliases | Quick shortcuts; functions are the scriptable version |
| Environment Variables | Passing data into scripts via the environment |
$PATH | Why scripts need absolute paths or a correct PATH |
| Pipes | Composing commands inside your scripts |
| Redirection | Capturing script output to files |
| bash / sh / zsh | Which interpreter your shebang should name |
Shell Scripting Cheatsheet
| Task | Syntax |
|---|---|
| Shebang | #!/bin/bash |
| Define a variable | name="value" |
| Read a variable | echo "$name" |
| Delete a variable | unset name |
| if/else | if [ ... ]; then ... else ... fi |
| File test | [ -f file ], [ -d dir ], [ -x cmd ] |
| for loop | for x in ...; do ... done |
| while loop | while [ ... ]; do ... done |
| Function | name() { ... } |
| Exit code | echo $? |
| Make executable | chmod +x script.sh |
| Run it | ./script.sh |