Back to Blog
Arnošt Havelka

Premium: Bash Script Variables

Master bash variables and arithmetic to build reusable automation scripts that adapt to different environments.

Start Interactive Lesson
Premium: Bash Script Variables

Premium: Bash Script Variables

Scripts 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

  1. Create variables with var='value' (no spaces around =).
  2. Reference variables with $var or ${var} for clarity.
  3. Do math with $((expression)).
  4. Use double quotes for interpolation: "prefix_$var_suffix".

Knowledge Check

1 / 2

What's the correct syntax to increment a variable?

References

These Microsoft Learn and Windows 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.