Chapter 53: Bash Variables
Bash Variables
Variables are one of the most powerful and most used things in Bash scripting — once you understand them, your scripts become smart, flexible, and reusable.
Think of a variable like a small labeled box in your kitchen:
- You put something inside the box (a value)
- You write a name on the label (the variable name)
- Later you can open the box anytime by saying the label name → take out the value
In Bash:
- You create a variable by giving it a name and putting a value inside
- You use it by writing $name or “$name” — Bash replaces it with whatever is inside the box
1. How to Create a Variable (The Golden Rule – NO SPACE Around = )
|
0 1 2 3 4 5 6 7 8 9 10 11 |
# Correct – NO space before or after = name="Webliance" age=25 city="Hyderabad" is_learning=true score=9.5 |
Wrong ways (these all fail):
|
0 1 2 3 4 5 6 7 8 |
name = "Webliance" # → bash: name: command not found age = 25 # same error name="Webliance " # trailing space becomes part of value |
Rule #1: No spaces around the = sign when assigning. Space makes Bash think “name” is a command.
2. How to Use / Read a Variable (Always Quote It!)
|
0 1 2 3 4 5 6 7 8 9 10 11 |
echo "Hello $name from $city!" echo "You are $age years old" # Better – quote to be safe echo "My name is $name" echo "My name is ${name}" # same thing, curly braces are optional but clearer sometimes |
Output:
|
0 1 2 3 4 5 6 7 8 |
Hello Webliance from Hyderabad! You are 25 years old My name is Webliance |
Why quote? Without quotes, if value has spaces → breaks:
|
0 1 2 3 4 5 6 7 8 |
folder="My Documents" ls $folder # wrong → ls My Documents → tries to ls two things ls "$folder" # correct → ls "My Documents" |
Golden rule #2: Always double-quote variables when using them: “$var” Especially if value might have spaces or be empty.
3. Special Tricks with Variables
Curly braces {} – When needed
|
0 1 2 3 4 5 6 7 8 |
name="Rahul" echo "$name123" # → nothing (looks for variable name123) echo "${name}123" # → Rahul123 (correct) |
Curly braces tell Bash: “this is the variable name, stop at }”
Common uses:
|
0 1 2 3 4 5 6 7 8 |
echo "Backup_${date}.tar.gz" # wrong – thinks ${date} is variable echo "Backup_${date}.tar.gz" # still wrong echo "Backup_${date}.tar.gz" # correct |
Default values – {var:-default}
|
0 1 2 3 4 5 6 7 |
folder="${BACKUP_FOLDER:-/home/webliance/backup}" echo "Saving to $folder" |
→ If BACKUP_FOLDER is not set → uses /home/webliance/backup
4. Built-in & Special Variables (You Use These Every Day)
| Variable | Meaning | Example output (yours will vary) |
|---|---|---|
| $HOME | Your home directory | /home/webliance |
| $USER | Your username | webliance |
| $PWD | Current working directory | /home/webliance/bash_class |
| $PATH | Colon-separated list of command search paths | /usr/local/bin:/usr/bin:/bin:… |
| $? | Exit status of last command (0 = success) | 0 |
| $$ | Process ID of current script | 12345 |
| $0 | Name of the script itself | ./my_script.sh |
| $1 $2… | Arguments passed to script | ./script.sh file1.txt → $1 = file1.txt |
| $# | Number of arguments passed | 3 |
| $@ / $* | All arguments as list | “$@” is safer (keeps spaces) |
5. Real Mini-Script Using Variables (Type This Now!)
Create file:
|
0 1 2 3 4 5 6 |
nano greet.sh |
Paste:
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
#!/bin/bash # Default values name="${1:-friend}" # $1 or "friend" if no argument city="${CITY:-Hyderabad}" # environment var or default echo "=====================================" echo " Namaste $name from $city! ☕ " echo " Welcome to Bash Variables Class! " echo "=====================================" echo "" echo "Current time : $(date '+%I:%M %p %Z')" echo "You are : $USER" echo "Home folder : $HOME" echo "Current folder : $PWD" echo "Number of files here: $(ls | wc -l)" echo "" echo "Script name : $0" echo "You ran it with : $# arguments" echo "First argument : $1" echo "Finished! Keep learning! 🚀" |
Save → make executable → run different ways:
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
chmod +x greet.sh ./greet.sh Rahul # → Namaste Rahul from Hyderabad! CITY=Delhi ./greet.sh # → Namaste friend from Delhi! ./greet.sh Priya Mumbai backup # → Namaste Priya from Hyderabad! (CITY not changed) |
6. Common Mistakes & How to Fix Them
| Mistake | Wrong code | Correct & safe | Why it matters |
|---|---|---|---|
| Space around = | name = “Webliance” | name=”Webliance” | Space = command |
| No quotes on variable | echo $path | echo “$path” | Word splitting |
| Forget curly braces | echo “$$ name_123” | echo ” $${name}_123″ | Variable name confusion |
| Use single quotes with variables | echo ‘Hello $name’ | echo “Hello $name” | No expansion |
| Forget export for child processes | MY_VAR=hello; bash other.sh | export MY_VAR=hello; bash other.sh | Child scripts can’t see |
Summary Table – Bash Variables Golden Rules
| Rule | Correct Example | Wrong Example |
|---|---|---|
| Assignment | name=”Webliance” | name = “Webliance” |
| Use variable | echo “$name” | echo $name |
| With spaces | path=”/home/webliance/My Folder” | path=/home/webliance/My Folder |
| Command output | today=$(date) | today=date (old style) |
| Default value | dir=”${BACKUP:-~/backup}” | — |
| Argument to script | echo “First file: $1” | echo First file: $1 |
Got it, boss? Variables are the memory boxes of your script — put values in, take them out with $, quote them always, and your scripts become alive and smart.
Any part confusing? Want next:
- “Teacher, explain local vs global variables & export”
- “How to do math with variables (( )) or expr)”
- “String operations (length, substring, replace)”
- “Arrays in Bash”
- “Read user input with read command”
Just say the topic — teacher is ready in Hyderabad! Keep practicing variables in small scripts — soon you’ll use them without thinking! 🐧📦🔤 😄
