•Arnošt Havelka
Premium: Bash Script Variables
Master bash variables and arithmetic to build reusable automation scripts that adapt to different environments.
Start Interactive LessonScripts that hardcode values are fragile. Learn to use variables to make your automation adapt to any situation.
Step 1: Assign and Echo Variables
Terminal
~$project='myapp'
echo $project
Variables store values you'll use multiple times. Store once, reuse forever.
Step 2: Use Variables in Arithmetic
Terminal
~$count=5
count=$((count + 1))
echo $count
The $((...)) syntax performs integer math. Perfect for counters, retries, and loops.
Step 3: Build Dynamic Commands with Variables
Terminal
~$backup_num=1
filename="backup_00$backup_num.tar.gz"
echo "Processing $filename"
Double quotes allow $ substitution. Your scripts now adapt without editing the code.
Checklist You Can Reuse
- Create variables with
var='value'(no spaces around=). - Reference variables with
$varor${var}for clarity. - Do math with
$((expression)). - Use double quotes for interpolation:
"prefix_$var_suffix".
Knowledge Check
1 / 2What's the correct syntax to increment a variable?
References
These documentation links provide authoritative details for the commands used in this article.
Up Next
Premium: Bash Script Functions
Write reusable functions to eliminate code duplication and build maintainable automation scripts.