Bash Variable Basics
Introduction to Shell Variables
In bash scripting, variables are fundamental storage units for holding data dynamically during script execution. They serve as essential components in linux programming, enabling developers to store, manipulate, and reference information efficiently.
Variable Definition and Assignment
Variables in bash can be defined using simple assignment syntax:
name="John Doe"
age=30
is_student=true
Variable Naming Conventions
Rule |
Example |
Description |
Start with letter/underscore |
_valid_name |
Must begin with letter or underscore |
No spaces around = |
count=5 |
Assignment requires no whitespace |
Case-sensitive |
Name vs name |
Distinct variables |
Types of Variables
graph TD
A[Variable Types] --> B[String]
A --> C[Integer]
A --> D[Boolean]
A --> E[Array]
String Variables
greeting="Hello, World!"
echo $greeting
Integer Variables
counter=100
total=$((counter + 50))
Boolean Variables
is_enabled=true
is_disabled=false
Array Variables
fruits=("apple" "banana" "cherry")
echo ${fruits[0]} ## Prints "apple"