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:

Bash

Examples:

Bash

Rule:

  • Options usually start with – or —
  • Arguments are what the command works on (files, folders, text, URLs…)
  • Spaces separate everything

Wrong syntax → error:

Bash

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:

Bash

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.

Bash

Common uses:

Bash

4. Variables – How to Create & Use Them

Syntax rules:

Bash

Special built-in variables (very useful):

Bash

5. Line Continuation – Long Commands on Multiple Lines

Use \ at end of line (backslash)

Bash

6. Redirection & Pipes – Very Important Syntax

Bash

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:

Bash

Run:

Bash

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! 🐧📜✍️ 😄

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *