2024年6月13日(木) 5:20 Angelo Borsotti <angelo.borso...@gmail.com>:
> This is not the same as debugging, for which set -x is devoted.
> "set -x" makes the ensuing commands be printed, but prefixed
> with "+ ", which makes the result look ugly,

PS4= (as Greg has replied)

> not to mention that
> the following "set +x" is echoed too (there are hacks to suppress
> the "set +x" output, but they are just hacks).

What are the hacks? What are the problems with them? You can have aliases:

alias @echo-on='set -x'
alias @echo-off='{ set +x; } 2>/dev/null'

> I would stress the importance of this: the purpose of scripts is
> to execute commands, informing the caller of what they execute,
> purged of all the builtins and other calculations the script does
> in order to come to the commands.

I don't think the purpose of scripts is to execute only the external
commands in general. The tasks implemented by builtins and other
calculations are allowed to be the purpose of scripts.

> Many solutions are posted in the internet, all of them are hacks with
> lots of edge cases that make them fail in presence of commands
> containing special characters, redirections, substitutions, etc.

Have you tried `set -v'? `set -v' is not a hack, (though there doesn't
seem to be a way to suppress echoing set +v).

The detailed design of how the logged commands should be filtered and
formatted depends on the purpose of each user. We already have `set
-x' and `set -v'. We can adjust the format by PS4 for `set -x'. It's
unrealistic to add a new option for every new preference of detailed
filtering and formatting. If it is really needed, you can implement
your filtering through the DEBUG trap, but it would finally be best to
explicitly write `echo COMMAND' before every COMMAND that you think is
important (unless you are lazy). It's stable and flexible.

--
Koichi

Reply via email to