Bash Variables and Data Types
A variable is a named container that stores a value. Instead of writing the same value multiple times in a script, store it once in a variable and use the variable name everywhere. When the value needs to change, update it in one place.
Creating a Variable
In Bash, a variable is created by writing the name, an equals sign, and the value. There must be no spaces around the equals sign.
#!/bin/bash name="Alice" age=25 city="Delhi"
Correct vs Incorrect:
name="Alice" ✓ Correct name = "Alice" ✗ Wrong – spaces around = cause an error
Reading a Variable Value
To use the value stored in a variable, put a $ sign before the variable name.
#!/bin/bash name="Alice" echo $name
Output:
Alice
Using Variables in Sentences
Variables can be placed inside double-quoted strings. Bash replaces the variable name with its value automatically.
#!/bin/bash name="Alice" city="Delhi" echo "Hello, $name. Welcome to $city."
Output:
Hello, Alice. Welcome to Delhi.
Variable Naming Rules
| Rule | Example |
|---|---|
| Only letters, numbers, and underscores | first_name, score1 |
| Cannot start with a number | 1name is invalid |
| Case-sensitive | Name and name are two different variables |
| No spaces allowed in name | first name is invalid |
Data Types in Bash
Bash treats everything as a string by default. However, there are three practical categories of data a variable can hold.
┌──────────────────────────────────────────────┐
│ Bash Variable Types │
│ │
│ 1. String → text="Hello World" │
│ 2. Integer → count=10 │
│ 3. Array → colors=("red" "green" "blue")│
└──────────────────────────────────────────────┘
String Variable
#!/bin/bash message="Learning Bash is fun" echo $message
Output:
Learning Bash is fun
Integer Variable
Bash stores numbers as strings too, but the declare -i option enforces integer behavior.
#!/bin/bash declare -i total=50 echo $total
Types of Variables in Bash
1. Local Variables
A local variable is created in the current shell session and is not available to other scripts or programs.
#!/bin/bash greeting="Good Morning" echo $greeting
2. Environment Variables
Environment variables are available to all programs and child processes started from the current shell. Use export to create one.
#!/bin/bash export APP_NAME="eStudy247" echo $APP_NAME
┌─────────────────────────────────────────────┐ │ Local vs Environment Variable │ │ │ │ Local: available in current script │ │ Environment: available to all child │ │ processes and subshells │ └─────────────────────────────────────────────┘
3. Read-Only Variables
A read-only variable cannot be changed once set. Use readonly to create one.
#!/bin/bash readonly PI=3.14159 echo $PI PI=5 # This will cause an error
Output:
3.14159 bash: PI: readonly variable
Bash Built-in (Special) Variables
Bash provides several special variables that are automatically set by the system.
| Variable | Meaning |
|---|---|
$0 | Name of the current script |
$1, $2, ... | Arguments passed to the script |
$# | Total number of arguments passed |
$@ | All arguments as separate words |
$? | Exit status of the last command (0 = success) |
$$ | Process ID (PID) of the current shell |
$USER | Currently logged-in username |
$HOME | Home directory of the current user |
$PWD | Current working directory |
Example Using Special Variables
#!/bin/bash echo "Script name: $0" echo "First argument: $1" echo "Total arguments: $#" echo "Current user: $USER" echo "Home directory: $HOME"
Run the script and pass two arguments:
./myscript.sh John 30
Output:
Script name: ./myscript.sh First argument: John Total arguments: 2 Current user: john Home directory: /home/john
Command Substitution – Storing Command Output in a Variable
The output of a command can be stored in a variable using $(command) syntax.
#!/bin/bash today=$(date) echo "Today is: $today"
Output:
Today is: Sat Apr 18 10:00:00 UTC 2026
Another example – count files in a directory:
#!/bin/bash filecount=$(ls | wc -l) echo "Number of files: $filecount"
Unset a Variable
Use unset to delete a variable and free its value from memory.
#!/bin/bash color="blue" echo $color # prints blue unset color echo $color # prints nothing (empty)
Key Takeaways
- Variables store values — use
$to read them. - No spaces around
=when assigning a value. - Use
exportto make a variable available to child processes. - Use
readonlyto prevent a variable from being changed. - Special variables like
$1,$?, and$USERare set automatically by Bash. - Use
$(command)to store command output in a variable.
