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 = )

Bash

Wrong ways (these all fail):

Bash

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!)

Bash

Output:

text

Why quote? Without quotes, if value has spaces → breaks:

Bash

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

Bash

Curly braces tell Bash: “this is the variable name, stop at }”

Common uses:

Bash

Default values – {var:-default}

Bash

→ 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:

Bash

Paste:

Bash

Save → make executable → run different ways:

Bash

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! 🐧📦🔤 😄

You may also like...

Leave a Reply

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