Chapter 51: Bash Syntax
Bash Syntax
Bash syntax means the correct rules and grammar you must follow when writing commands — whether you type them directly in the terminal (interactive mode) or put them inside a script file (scripting mode).
If you break the syntax rules → Bash gets confused and throws errors like:
- syntax error near unexpected token
- unexpected end of file
- command not found
- permission denied (sometimes syntax-related)
Think of Bash syntax like Hindi/English grammar rules:
- Subject + Verb + Object
- Correct punctuation
- Quoting words with spaces
- Special characters have special meanings
Let’s learn the most important syntax rules step-by-step — with tons of examples, wrong vs correct, and why things break.
1. Basic Command Structure (The Foundation)
A normal command is:
|
0 1 2 3 4 5 6 |
command [options] [arguments] |
Examples:
|
0 1 2 3 4 5 6 7 8 9 10 |
ls # command only ls -la # command + options ls -la ~/Documents # command + options + argument mkdir my_folder # command + argument cp file1.txt backup/ # command + source + destination |
Rule:
- Options usually start with – or —
- Arguments are what the command works on (files, folders, text, URLs…)
- Spaces separate everything
Wrong syntax → error:
|
0 1 2 3 4 5 6 7 |
ls-la # wrong - no space between ls and -la mkdirmy_folder # wrong - no space |
2. Quoting – The #1 Source of Confusion
Anything with spaces or special characters must be quoted.
| Type of quote | When to use | Example (correct) | What happens without quotes |
|---|---|---|---|
| Double quotes ” “ | Variables and $(commands) should expand | echo “My home is $HOME” | echo My home is $HOME → literal text |
| Single quotes ‘ ‘ | Prevent all expansion (literal text) | echo ‘My home is $HOME’ | echo ‘My home is $HOME’ → no variable |
| No quotes | Only when no spaces/special chars | echo Hyderabad | echo My name → syntax error (two arguments) |
Real examples:
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
# Correct echo "Today is $(date)" name="Webliance" echo "Hello $name from Hyderabad" # Wrong – breaks echo Today is $(date) # Today is: command not found echo Hello $name from Hyderabad # Hello Webliance from Hyderabad (but fragile) # Use single quotes when you want literal $ echo 'Price is $50' # Price is $50 (no variable expansion) |
Golden rule: Always quote variables and anything with spaces → “$var” “$file” “$(date)”
3. Command Substitution – $( ) or
Run a command and use its output inside another command.
|
0 1 2 3 4 5 6 7 8 9 10 11 |
# Modern & best way (preferred) today=$(date +%Y-%m-%d) echo "Backup made on $today" # Old way (backticks) – still works but avoid echo "Backup made on `date +%Y-%m-%d`" |
Common uses:
|
0 1 2 3 4 5 6 7 8 9 10 |
files_count=$(ls | wc -l) echo "You have $files_count files here" backup_file="docs_$(date +%F).tar.gz" tar -czf "$backup_file" Documents/ |
4. Variables – How to Create & Use Them
Syntax rules:
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
# Correct – NO space around = name="Webliance" age=25 city="Hyderabad" # Wrong – spaces break it name = "Webliance" # → command not found: name # Use $ to read variable echo "Hi $name, you are $age years old" # Always quote when using echo "$name lives in $city" |
Special built-in variables (very useful):
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 |
$HOME → your home folder $USER → your username $PWD → current directory (same as pwd) $PATH → list of folders where commands are searched $$ → current script's process ID $? → exit status of last command (0 = success) $0 → name of the script itself $1 $2 $3... → arguments passed to script |
5. Line Continuation – Long Commands on Multiple Lines
Use \ at end of line (backslash)
|
0 1 2 3 4 5 6 7 8 9 |
tar -czvf very_long_backup_name_$(date +%Y-%m-%d).tar.gz \ --exclude='*.log' \ --exclude='node_modules' \ Documents/ Downloads/ photos/ |
6. Redirection & Pipes – Very Important Syntax
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
# Redirect output to file (overwrite) echo "Hello" > file.txt # Append echo "More text" >> file.txt # Pipe (send output to next command) ls -la | grep ".sh" # Both together df -h | grep "/dev/sda" > disk_usage.txt |
7. Common Syntax Traps & Errors (You Will See These!)
| Wrong syntax | Error you get | Correct version |
|---|---|---|
| echo $namefromHyderabad | namefromHyderabad: command not found | echo “$name from Hyderabad” |
| if [ $age > 18 ] | [: too many arguments | if [ “$age” -gt 18 ] |
| var = value | var: command not found | var=value |
| for i in 1 2 3 | syntax error near unexpected token | for i in 1 2 3; do … done |
| grep “error” log.txt | wc -l | works but fragile | grep “error” log.txt |
Your First Mini-Script Using Syntax Rules
Create info.sh:
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
#!/bin/bash # All variables quoted properly current_date="$(date +'%Y-%m-%d %H:%M:%S')" username="$USER" current_dir="$PWD" echo "====================================" echo "System Info for $username" echo "Date & Time : $current_date" echo "Current Dir : $current_dir" echo "Home Folder : $HOME" echo "Bash Version: $BASH_VERSION" echo "====================================" # Pipe example echo "Number of .sh files here:" ls -1 | grep "\.sh$" | wc -l |
Run:
|
0 1 2 3 4 5 6 7 |
chmod +x info.sh ./info.sh |
Quick Summary Table – Bash Syntax Golden Rules
| Rule | Correct Example | Why it matters |
|---|---|---|
| No spaces around = in variables | name=”Webliance” | Space makes it a command |
| Always quote variables | echo “$name” | Prevents word splitting |
| Use $$ ( ) for command substitution | today= $$(date) | Modern, readable, nestable |
| Quote anything with spaces | mv “My File.txt” backup/ | Otherwise splits into two arguments |
| End continued lines with \ | command –long-option=value \ | Breaks long lines cleanly |
| Spaces around [ ] in if/test | if [ “$var” = “yes” ]; then | [ is a command, needs spaces |
| ; or new line after do / then | for i in 1 2; do echo $i; done | Separates statements |
Got the grammar now? Bash syntax is just a small set of rules — once you follow them, your commands and scripts become bulletproof.
Any part confusing? Want next: “Teacher, explain variables & quoting in more depth” or “if/else syntax” or “for loops” or “how to debug syntax errors”?
Just say — teacher is ready in Hyderabad! Keep practicing — type slowly, check ls -l, quote everything, and soon syntax will feel natural! 🐧📜✍️ 😄
