Post by Mike McClainPost by Greg WooledgePost by Peng Yu-v Print shell input lines as they are read.
Which is almost entirely useless. It doesn't help you debug a script.
I don't know why people keep trying to use it.
I could see it maybe possibly helping Chet debug bash. But not a script.
Hi Greg,
While I don't doubt for a minute that you know what you're talking
about, I don't.
How is this different than an echo ""? The bash version of the
proverbial 'print statement' used by programmers everywhere?
Bash (and sh) has two different "debug" options: -x and -v.
set -x tells bash (or sh) to print each command as it is being
*executed*, showing parameter expansions. It is a useful tool.
set -v tells bash (or sh) to print each *line* of the script as it
is being *read*. But this is not useful, because the point at which
a line of input is read has nothing to do with when the commands
contained in that line are executed.
As Peng partially demonstrated, any long compound command (e.g. a
function definition, or a while loop) is read all at once, before
any execution happens. set -v shows you the shell reading the lines
of the script. So what? It's not executing them. It's just reading
them. It doesn't reveal any useful information.
Adding echo/printf commands to show the contents of variables (or the
simple fact that execution has reached a specific point) is a useful tool,
and you should continue doing so wherever it's helpful. This is similar
to how set -x helps you -- it shows you what the shell is actually doing.
"declare -p varname" is also useful for showing the contents of
variables in many cases. In a very small number of cases, even that
may not reveal enough information, and then something like
printf %s "$varname" | od -c -An
(or your favorite hexdumper) may be what's needed.