help-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Help-bash] `set -v` difference in () vs not in ()


From: Greg Wooledge
Subject: Re: [Help-bash] `set -v` difference in () vs not in ()
Date: Tue, 12 Jun 2018 08:10:27 -0400
User-agent: NeoMutt/20170113 (1.7.2)

On Mon, Jun 11, 2018 at 08:43:34PM -0700, Mike McClain wrote:
> On Mon, Jun 11, 2018 at 08:27:24AM -0400, Greg Wooledge wrote:
> > On Sat, Jun 09, 2018 at 05:32:23PM -0500, Peng Yu wrote:
> > > -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.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]