How to Watch the Value of a Variable While Running a Script

The easiest way to do that is to put 'echo $variable' in the script to output the variable or using 'set -x' and grepping for '=' in the debug output.

You also can use 'echo $variable >; /tmp/somefile' - so /tmp/somefile contains the current value of the variable.

Simply use the commands 'set' or 'export' in the script to dump all variables or the exported variables of the shell process.

If you know the PID of the running shell script, you can also type:

tr '\000' '\n' < /proc/PID/environ

to dump all exported variables of the shell process without modifying the script.

The last method can also be used to 'monitor' one variable. The variable needs to be exported to do that. If we want to watch the exported variable 'dummy' in the shell process with the PID 123:

    while : ; do
        tr '\000' '\n' < /proc/123/environ | grep '^dummy='
        sleep 1