🧩 Bash Basics & Syntax – Variables, Quoting, Arrays & Prompt Navigation
🧲 Introduction – Build Your Foundation with Bash Syntax
Mastering Bash begins with understanding its core syntax—how commands are structured, how variables work, and how to handle data with arrays and substitutions. This knowledge forms the backbone of writing reliable and flexible Bash scripts.
🎯 In this guide, you’ll learn:
- How to use and export shell variables
- What special variables like $?,$0, and$@do
- How Bash treats single quotes vs double quotes
- How to substitute commands and assign outputs
- How to work with arrays and key-value pairs
- How to navigate and interact with the shell prompt
📘 Topics Covered
| 🔹 Topic | 📖 Description | 
|---|---|
| 🟢 Bash: Prompt & Navigation | Learn shell prompt basics and command line movement | 
| 🟢 Bash: Using Variables ($var, export, unset) | Declare, use, export, and unset variables | 
| 🟢 Bash: Special Variables ($?, $#, $@, $0, $1, etc.) | Built-in variables for script metadata and status | 
| 🟢 Bash: Quoting Mechanisms (‘, “, \) | Differences between single, double, and escaped quotes | 
| 🟢 Bash: Shell Substitutions ( $(…),…) | Use command substitution for dynamic scripting | 
| 🟢 Bash: Arrays & Associative Arrays | Declare and use indexed or key-value arrays | 
🟢 Bash: Prompt & Navigation
The Bash prompt (PS1) lets you interact with the shell.
pwd          # Print current working directory
cd /home     # Change directory
ls -l        # List directory contents
history      # Show command history
!!           # Repeat last command
✅ Use Tab for autocomplete and Ctrl + R for reverse history search.
🟢 Bash: Using Variables ($var, export, unset)
🔹 Assign & Access:
name="Raspberry Pi"
echo $name
🔹 Export for Subshells:
export PI_USER="pi"
🔹 Unset:
unset name
✅ No spaces around = when assigning.
🟢 Bash: Special Variables ($?, $#, $@, $0, $1…)
| Variable | Purpose | Example Use | 
|---|---|---|
| $? | Exit status of last command | echo $? | 
| $0 | Script name | echo $0 | 
| $1 | First argument | echo $1 | 
| $# | Number of arguments | echo $# | 
| $@ | All arguments (as separate strings) | for i in "$@"; do …; done | 
| $* | All arguments (as single string) | echo "$*" | 
✅ Use "${@}" in quotes to preserve whitespace.
🟢 Bash: Quoting Mechanisms (‘, “, \)
🔹 Single Quotes ':
Preserve literal value (no expansion):
echo '$USER'   # prints $USER
🔹 Double Quotes ":
Allow variable and command expansion:
echo "User is $USER"
🔹 Backslash \:
Escape individual characters:
echo \$HOME
✅ Use quotes properly to handle spaces, variables, and special characters safely.
🟢 Bash: Shell Substitutions ($(…), …)
🔹 Syntax:
today=$(date)
echo "Today is $today"
🔹 Legacy Format (not recommended):
echo "Now is `date`"
✅ Use $(command) for modern, nested-safe scripting.
🟢 Bash: Arrays & Associative Arrays
🔹 Indexed Array:
arr=(apple banana cherry)
echo ${arr[1]}       # banana
echo ${#arr[@]}      # 3
🔹 Looping Through:
for item in "${arr[@]}"; do
  echo $item
done
🔹 Associative Array (key-value):
declare -A capitals
capitals[India]="Delhi"
capitals[France]="Paris"
echo ${capitals[France]}
✅ Use declare -A to create associative arrays before assigning.
📌 Summary – Bash Basics & Syntax
Understanding Bash’s syntax unlocks the ability to automate tasks, process user input, and build reusable scripts. Whether you’re creating system tools or automation pipelines, Bash syntax mastery is key.
🔍 Key Takeaways:
- Use exportto pass variables to child processes
- Special variables like $0,$?, and$@give you runtime metadata
- Quoting determines how Bash interprets values
- Use $(…)for command output substitution
- Arrays and associative arrays help manage structured data
⚙️ Real-World Applications:
- Scripting command-line tools
- Configuring startup and environment variables
- Managing argument-driven shell automation
- Processing lists and key-value configurations
❓ FAQ: Bash Basics & Syntax
❓ What’s the difference between $* and $@?
✅ $@ treats quoted arguments individually; $* joins them into one string.
❓ How do I export a variable globally?
✅ Use export var=value. It’s available to child processes of the shell.
❓ Can I nest command substitutions?
✅ Yes:
echo "Today is $(date +%A)"
❓ What’s the best way to store multiple values in Bash?
✅ Use arrays:
arr=(one two three)
Share Now :
