help-bash
[Top][All Lists]
Advanced

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

Re: Set verbose Flag Inside if-block


From: Chet Ramey
Subject: Re: Set verbose Flag Inside if-block
Date: Sun, 16 Apr 2023 17:09:56 -0400
User-agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:102.0) Gecko/20100101 Thunderbird/102.9.1

On 4/16/23 4:46 PM, Sysadmin Lists wrote:

That's not what's happening. Breaking up that command line into newlines shows
the bug.  The first version should echo everything inside the if-statement 
after the
   'set -ve' but doesn't.

It should not.

It should. It'd be useful to only echo commands inside the if-block if we get 
there.

It's simply not how the shell works. I'm not sure if you really understand
the difference between the shell's reading input, parsing that input into
commands, and executing those commands. The shell is not a read-and-
execute-on-the-fly engine. Let me see if I can explain that below.


It's treating the entire if-block as a single command.

The if command is a single compound command that is read and parsed in its
entirety before any of it is executed.

Why can't the same logic used for the -x flag apply to the -v flag here? It'd 
make sense.

OK. `if' is a compound command. A compound command is a single syntactic
unit in terms of parsing and execution. This means that the entire `if'
command, from `if' to `fi', is read and parsed before the compound command
is executed. (How else would you deal with syntax errors?) You can see
where this is going: the entire body of the `if' command is read and parsed
before even the condition is executed.

The `-v' option affects behavior as a command is read. Since compound
commands are read and parsed in their entirety before any execution, a
`set -v' in the body of a compound command cannot affect the behavior of
that command: by the time the verbose option is enabled, the entire `if'
compound command has already been read and parsed.

The `-x' option, on the other hand, affects behavior as a command is
executed. As soon as it's enabled, the shell enables execution tracing for
subsequently-executed commands. Since it takes effect as a command is
executing, even a compound command, it can affect commands following the
`set -x' in the body of the compound command.

--
``The lyf so short, the craft so long to lerne.'' - Chaucer
                 ``Ars longa, vita brevis'' - Hippocrates
Chet Ramey, UTech, CWRU    chet@case.edu    http://tiswww.cwru.edu/~chet/




reply via email to

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