Debugging Scripts
Variable Probe
Split a Debug Value
A probe line prints the values a script is about to use. Parameter expansion can split a compact debug value into named parts.
Program
Play the script to choose a probe value and see the parsed fields.
variable_probe.sh
Replay: real traced execution (multi-file project)
#!/usr/bin/env bash
probe="alpha:42"
name="${probe%%:*}"
count="${probe##*:}"
echo "probe:$name=$count"
#!/usr/bin/env bash
probe="beta:7"
name="${probe%%:*}"
count="${probe##*:}"
echo "probe:$name=$count"
probe ← alpha:42
3probe="alpha:42"4name="${probe%%:*}"values this stepalpha:42probename ← alpha
3probe="alpha:42"4name="${probe%%:*}"5count="${probe##*:}"values this stepalphanamealpha:42probecount ← 42
4name="${probe%%:*}"5count="${probe##*:}"6echo "probe:$name=$count"values this step42countalpha:42probeecho "probe:$name=$count"
5count="${probe##*:}"6echo "probe:$name=$count"outputprobe:alpha=42values this stepalphaname42count
probe ← beta:7
3probe="beta:7"4name="${probe%%:*}"values this stepbeta:7probename ← beta
3probe="beta:7"4name="${probe%%:*}"5count="${probe##*:}"values this stepbetanamebeta:7probecount ← 7
4name="${probe%%:*}"5count="${probe##*:}"6echo "probe:$name=$count"values this step7countbeta:7probeecho "probe:$name=$count"
5count="${probe##*:}"6echo "probe:$name=$count"outputprobe:beta=7values this stepbetaname7count
probe
A probe is a temporary diagnostic line that exposes values during development.
prefix trim
`${probe%%:*}` keeps the text before the first colon-shaped separator.
suffix trim
`${probe##*:}` keeps the text after the last colon-shaped separator.