help-bash
[Top][All Lists]
Advanced

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

Re: colored verbose


From: Greg Wooledge
Subject: Re: colored verbose
Date: Wed, 8 Nov 2023 09:13:57 -0500

On Wed, Nov 08, 2023 at 03:01:11PM +0100, lacsaP Patatetom wrote:
> bash's verbose mode makes it possible to "exploit" comments and "preserve"
> code (pipe characters don't disappear for example)

It also means that any compound commands are written by verbose mode
when the parser reads them, NOT when they're executed.  This affects
functions, loops, if/then, case, etc.

Consider something as simple as this:

#!/bin/bash -v
f() {
    echo "f has been called"
}

for i in 1 2 3; do
    sleep 1
    f
done

What happens when we run it:

unicorn:~$ ./bar
#!/bin/bash -v
f() {
    echo "f has been called"
}

for i in 1 2 3; do
    sleep 1
    f
done
f has been called
f has been called
f has been called
unicorn:~$ 

The entire script is a shebang, a compound command (function definition),
a blank line, and another compound command (for loop).  With verbose
mode, we see the entire script written out, because the parser has to
read it all before it can begin executing the loop.  Then we see the
three lines of output from the function's three calls.  We don't see the
sleep commands or the function calls or even the echo being executed.

With -x, we get this:

unicorn:~$ ./bar
+ for i in 1 2 3
+ sleep 1
+ f
+ echo 'f has been called'
f has been called
+ for i in 1 2 3
+ sleep 1
+ f
+ echo 'f has been called'
f has been called
+ for i in 1 2 3
+ sleep 1
+ f
+ echo 'f has been called'
f has been called
unicorn:~$ 

I'd argue that most people find that more useful than the -v version.

The ONLY time -v is ever useful is in the most trivial of scripts, where
there are no compound commands at all.  If the script is just a linear
sequence of commands, which are executed straight down the line, then -v
and -x are basically equivalent, and some people might prefer -v's
output, as it shows the redirections.  But most scripts are not that
simple.  In most real world applications, there'll be *some* sort of
flow control (a loop or a conditional), and then -v becomes far less
useful.



reply via email to

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