I like to use set -x
in scripts to show what's going on, especially if the script is going to run in a CI/CD pipeline and I might need to debug some failure post-hoc.
One annoyance with doing this is that if I want to echo some text to the user (e.g., a status message or "I'm starting to do $X"
) then that message gets output twice - once for the echo
command itself being echoed, and then once as the output of that echo
command.
What's a good way to make this nicer? One solution is this:
set -x
... bunch of normal commands that get echoed
(
Temporarily don't echo, so we don't double-echo
set +x
echo "Here is my status message"
)
... rest of commands get echoed again
But the two problems with that are
- That's a lot of machinery to write every time I want to tell the user something, and it's "non-obvious" enough that it probably requires the comment every time
- It echoes the
set +x
too, which is undesirable.
Is there another option that works well?
Something like Make's feature of prepending an @
to suppress echoing would be great, but I've not been able to find such a feature in Bash.
set -x
is a debugging tool, not something intended to build nice text interfaces. – chepner Nov 17 '22 at 16:36set -x
just because you don't want yourecho
s printed twice. – Marco Nov 18 '22 at 07:49