Chapter 54: Bash Data Types
Bash Data Types
This topic is super important because many beginners think “Bash has no data types — everything is string!” — and that’s half-true, but only half.
In reality, Bash is very weakly typed — almost everything is treated as a string by default, but Bash secretly knows a few other types behind the scenes and lets you use them in smart ways.
So let’s break it down like we’re sitting together in Hyderabad: clear, slow, with lots of examples you can type right now.
1. The Big Truth First: Bash is Mostly String-Based
In Bash:
- When you write var=123
- When you write var=”hello”
- When you write var=true
→ In all three cases, Bash stores the value as a string (text characters).
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
<span class="line">a=123</span> <span class="line">b="123"</span> <span class="line">c=hello</span> <span class="line">d="hello world"</span> <span class="line">echo "$a" # prints 123</span> <span class="line">echo "$b" # prints 123</span> <span class="line">echo "$c" # prints hello</span> <span class="line">echo "$d" # prints hello world</span> |
Bash does not care whether you used quotes or not when assigning — it always saves them as text.
So rule #1:
In Bash, all normal variables are strings by default.
But — and this is the important part — Bash can treat those strings as other types when you ask it to.
2. The Main “Types” Bash Understands (Even If It Stores Them as Strings)
3. Integers – The Most Common “Non-String” Type
Bash has a special way to tell it: “treat this variable as integer”
Two main ways:
Way 1: declare -i (integer)
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
<span class="line">declare -i age=25</span> <span class="line">declare -i score</span> <span class="line">score=95</span> <span class="line">echo "Age is $age" # 25</span> <span class="line">((age = age + 5)) # math works without $</span> <span class="line">echo "Next year: $age" # 30</span> <span class="line">score+=10 # also works</span> <span class="line">echo $score # 105</span> |
If you try to put non-number:
|
0 1 2 3 4 5 6 7 |
<span class="line">age="twenty five" # → warning: value is not integer</span> <span class="line">age=twenty # → error or 0</span> |
Way 2: Double parentheses (( )) – Arithmetic evaluation
This is the most popular way in scripts — no need for declare -i
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
<span class="line">count=10</span> <span class="line">((count = count + 1)) # count = 11</span> <span class="line">((count++)) # count = 12</span> <span class="line">((count *= 2)) # count = 24</span> <span class="line">echo $count</span> <span class="line"># Compare numbers</span> <span class="line">(( count > 20 )) && echo "More than 20!"</span> |
Inside (( )):
- No $ needed for variables
- You can do math like C: +, -, *, /, %, **, >, <, ==, !=, &&, ||, etc.
4. Arrays – List of Values
Bash has indexed arrays (0,1,2…) and associative arrays (key-value)
Indexed array (normal list)
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
<span class="line"># Way 1</span> <span class="line">fruits=(apple banana "mango shake" orange)</span> <span class="line"># Way 2</span> <span class="line">colors[0]="red"</span> <span class="line">colors[1]="blue"</span> <span class="line">colors[5]="yellow" # gaps are allowed</span> <span class="line">echo ${fruits[0]} # apple</span> <span class="line">echo ${fruits[2]} # mango shake</span> <span class="line">echo ${#fruits[@]} # 4 (length)</span> <span class="line">echo ${fruits[*]} # all elements</span> |
Loop over array:
|
0 1 2 3 4 5 6 7 8 |
<span class="line">for fruit in "${fruits[@]}"; do</span> <span class="line"> echo "I like $fruit"</span> <span class="line">done</span> |
Associative array (key → value) – Bash 4+
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
<span class="line">declare -A person</span> <span class="line">person[name]="Priya"</span> <span class="line">person[city]="Hyderabad"</span> <span class="line">person[age]=28</span> <span class="line">person[skills]="Bash Python AWS"</span> <span class="line">echo "Name: ${person[name]}"</span> <span class="line">echo "City: ${person[city]}"</span> <span class="line">echo "All keys: ${!person[@]}"</span> <span class="line">echo "All values: ${person[@]}"</span> |
5. “Boolean” in Bash – No Real Boolean Type
Bash has no true boolean — people use conventions:
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
<span class="line"># Convention 1: string "true"/"false"</span> <span class="line">success=true</span> <span class="line">if [ "$success" = "true" ]; then</span> <span class="line"> echo "Yes!"</span> <span class="line">fi</span> <span class="line"># Convention 2: 0 = success, non-zero = fail (like exit codes)</span> <span class="line">status=0</span> <span class="line">if (( status == 0 )); then</span> <span class="line"> echo "Command succeeded"</span> <span class="line">fi</span> |
Most common pattern:
|
0 1 2 3 4 5 6 7 8 9 10 |
<span class="line">if grep -q "error" log.txt; then</span> <span class="line"> echo "Found error"</span> <span class="line">else</span> <span class="line"> echo "All good"</span> <span class="line">fi</span> |
Quick Practice Right Now (Type These!)
|
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 |
<span class="line"># 1. String</span> <span class="line">city="Hyderabad"</span> <span class="line">echo "I live in $city"</span> <span class="line"># 2. Integer</span> <span class="line">declare -i marks=85</span> <span class="line">((marks += 5))</span> <span class="line">echo "New marks: $marks"</span> <span class="line"># 3. Array</span> <span class="line">files=(doc1.txt doc2.pdf photo.jpg)</span> <span class="line">echo "Second file: ${files[1]}"</span> <span class="line">echo "Total files: ${#files[@]}"</span> <span class="line"># 4. Associative</span> <span class="line">declare -A info</span> <span class="line">info[username]="$USER"</span> <span class="line">info[date]=$(date +%Y-%m-%d)</span> <span class="line">echo "User: ${info[username]} on ${info[date]}"</span> |
Summary Table – Bash “Data Types” Cheat Sheet
Got it, boss? Bash variables are mostly strings, but Bash lets you treat them as integers, arrays, or key-value maps when you need — just remember to quote them properly and use the right syntax for math and arrays.
Any part confusing? Want next:
- “Teacher, explain parameter expansion {var} tricks”
- “Math in Bash – (( )) vs expr vs bc”
- “How to handle arrays in loops”
- “String manipulation (length, substring, replace)”
- “Environment variables & export”
Just say the topic — teacher is ready in Hyderabad! Keep playing with variables in your scripts — soon they’ll feel like your best friends! 🐧📊🔢 😄
