Bash variables hold strings, numbers, paths, and command results. Arithmetic expansion lets a script calculate with integer values.

Program

Play the script to watch named values build a small checkout total step by step.

variables.sh
Replay: real traced execution (multi-file project)
#!/usr/bin/env bash

price=18
quantity=3
subtotal=$((price * quantity))
discount=5
total=$((subtotal - discount))
echo "total=$total"
  1. price ← 18

    3price=184quantity=3
    values this step18price
  2. quantity ← 3

    3price=184quantity=35subtotal=$((price * quantity))
    values this step3quantity
  3. subtotal ← 54

    4quantity=35subtotal=$((price * quantity))6discount=5
    values this step54subtotal18price3quantity
  4. discount ← 5

    5subtotal=$((price * quantity))6discount=57total=$((subtotal - discount))
    values this step5discount
  5. total ← 49

    6discount=57total=$((subtotal - discount))8echo "total=$total"
    values this step49total54subtotal5discount
  6. echo "total=$total"

    7total=$((subtotal - discount))8echo "total=$total"
    outputtotal=49
    values this step49total

Follow the Total

  1. price starts at 18.
  2. quantity starts at 3.
  3. subtotal is 18 * 3, which becomes 54.
  4. discount is 5.
  5. total is 54 - 5, so stdout is total=49. | name | value | | --- | --- | | price | 18 | | quantity | 3 | | subtotal | 54 | | discount | 5 | | total | 49 |
assignment An assignment stores text in a shell variable with no spaces around `=`.
arithmetic expansion `$((...))` evaluates integer arithmetic before assignment.

Exercise: variables.sh

Reproduce total=49, then point to the two visible values that make subtotal and the one value subtracted as the discount.