| 1 | #!/bin/bash |
| 2 | |
| 3 | trap 'echo "VARIABLE-TRACE> \$variable = \"$variable\""' DEBUG |
| 4 | # Echoes the value of $variable after every command. |
| 5 | |
| 6 | variable=29; line=$LINENO |
| 7 | |
| 8 | echo " Just initialized \$variable to $variable in line number $line." |
| 9 | |
| 10 | let "variable *= 3"; line=$LINENO |
| 11 | echo " Just multiplied \$variable by 3 in line number $line." |
| 12 | |
| 13 | exit 0 |
| 14 | |
| 15 | # The "trap 'command1 . . . command2 . . .' DEBUG" construct is |
| 16 | #+ more appropriate in the context of a complex script, |
| 17 | #+ where inserting multiple "echo $variable" statements might be |
| 18 | #+ awkward and time-consuming. |
| 19 | |
| 20 | # Thanks, Stephane Chazelas for the pointer. |
| 21 | |
| 22 | |
| 23 | Output of script: |
| 24 | |
| 25 | VARIABLE-TRACE> $variable = "" |
| 26 | VARIABLE-TRACE> $variable = "29" |
| 27 | Just initialized $variable to 29. |
| 28 | VARIABLE-TRACE> $variable = "29" |
| 29 | VARIABLE-TRACE> $variable = "87" |
| 30 | Just multiplied $variable by 3. |
| 31 | VARIABLE-TRACE> $variable = "87" |