On Thu, Jun 13, 2024 at 10:01:16AM +0200, Angelo Borsotti wrote:
> @echo-on
> cat f1.txt f1.txt > f1.tmp
> @echo-off
> 
> I.e. the command is not entirely displayed.

Yeah.  This is what I mentioned originally: set -x does not show
redirections.  Ever.  There is no workaround for this currently.
A new feature would have to be implemented, or you'll just have to
live without it.

> P.S. we can replace "set -x" by "set +v":
> alias @echo-on='set -v'
> alias @echo-off='{ set +v; } 2>/dev/null'
> 
>  This shows properly the command, but also shows @echo-off. I.e.
> '{ set +v; } 2>/dev/null'  shows itself.
> 
> I have no idea how to suppress this.

The problem is deeper than that.  set -v doesn't show *commands* as
they are executed.  It shows *lines* as they are *read from the script*
by the shell.

If your script has any kind of compound commands, such as functions,
loops, if/then, or case statements, set -v shows the lines of the script
being read, but does *not* show each simple command within the compound
command as it's being executed.

#!/bin/sh 
set -v
for i in 1 2 3; do
  echo "This is command number $i"
done

Here, for example, set -v shows you the lines of the loop as they are
read, all at once, and then the loop is executed, during which time you
see only the output from echo, and nothing at all from set -v.

Here's another example, with a conditional branch:

#!/bin/sh 
set -v
if true; then
  : run one thing
else
  : run another thing
fi

When you run this, you'll see all the lines of the if statement written
by set -v, but you have no way to see which branch was actually taken.

One final example, with no explanations.  I'll let you try to figure out
what's happening here:


hobbit:~$ cat foo
#!/bin/sh 
f() {
  set -v
  echo v has been turned on
  set +v
}
f
hobbit:~$ ./foo
v has been turned on


For nontrivial scripts, set -v is almost never useful.

Reply via email to