Next: , Previous: , Up: Debugger Command Reference   [Contents][Index]


4.9 Examining Data (‘print’, ‘examine’, ‘info variables’)

One way to examine string data in your script is with the print command (abbreviated p). However a more versatile print command is x; it can print variable and function definitions and can do arithmetic computations. Finally, the most general method would be via eval echo.

print expr

Use print to display strings as you would from echo. And as such, variable names to be substituted have to be preceded with a dollar sign. As with echo, filename expansion, e.g. tilde expansion, is performed on unquoted strings. So for example if you want to print a *, you would write ‘print "*"’, not ‘print *’. If you want to have the special characters dollars sign appear, use a backslash.

bashdb<0> print the value of x is $x
the value of x is 22
bashdb<1> p The home directory for root is ~root
The home directory for root is /root
bashdb<2> p ’*** You may have won $$$ ***’
*** You may have won $$$ ***
bashdb<3> # Note the use of the single quotes.
bashdb<3> # Compare what happens with double quotes or no quotes
print
p

If you omit expr, the BASH debugger displays the last expression again.

x variable1 [variable2...]
x expr

This is a smarter, more versatile “print” command, and although sometimes it might not be what you want, and you may want to resort to either print or eval echo....

As with print, if you omit expr, the BASH debugger displays the last expression again.

The x command first checks if expr is variable or a list of variables delimited by spaces. If it is, the definition(s) and value(s) of each printed via BASH’s declare -p command. This will show the variable’s attributes such as if it is read only or if it is an integer. If the variable is an array, that is show and the array values are printed.

If instead expr is a function, the function definition is printed via BASH’s declare -f command. If expr was neither a variable nor an expression, then we try to get a value via let. And if this returns an error, as a last resort we call print and give what it outputs.

Since let may be used internally and since (to my thinking) let does funny things, the results may seem odd unless you understand the sequence tried above and how let works. For “example if the variable foo has value 5, then ‘x foo’ shows the definition of foo with value 5, and ‘x foo+5’ prints 10 as expected. So far so good. However if foo is has the value ‘alpha’, ‘x foo+5’ prints 5 because let has converted the string ‘alpha’ into the numeric value 0. So ‘p foo+5’ will simply print “foo+5”; if you want the value of “foo” substituted inside a string, for example you expect “the value of foo is $foo” to come out “the value of foo is 5”, then the right command to use is print rather than x, making sure you add the dollar onto the beginning of the variable.

bashdb<0> examine x y
declare -- x="22"
declare -- y="23"
bashdb<1> examine x+y
45
bashdb<2> x fn1
fn1 ()
{
    echo "fn1 here";
    x=5;
    fn3
}
bashdb<2> x FUNCNAME
declare -a FUNCNAME='([0]="_Dbg_cmd_x" [1]="_Dbg_cmdloop" [2]="_Dbg_debug_trap_handler" [3]="main")'
V [!][pattern]

If you want to all list variables and values or a set of variables by pattern, use this command.

bashdb<0> V dq*
dq_args="dq_*"
dq_cmd="V"
bashdb<1> V FUNCNAME
FUNCNAME='([0]="_Dbg_cmd_list_variables" [1]="_Dbg_cmdloop" [2]="_Dbg_debug_trap_handler" [3]="main")'

Next: , Previous: , Up: Debugger Command Reference   [Contents][Index]