•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
Command Prompt
myapp
C:\Users\User>echo 'myapp'
Variables store values you'll use multiple times. Store once, reuse forever.
Step 2: Use Variables in Arithmetic
Command Prompt
5
C:\Users\User>echo 5
The $((...)) syntax performs integer math. Perfect for counters, retries, and loops.
Step 3: Build Dynamic Commands with Variables
Command Prompt
Processing backup_001.tar.gz
C:\Users\User>echo 'Processing backup_001.tar.gz'
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.