21

In bash, when running with the -x option, is it possible to exempt individual commands from echoing?

I'm trying to make the output as neat as possible, so I am running certain parts of my script in a subshell with set +x. However, the row set +x itself is still echoed and adds no valuable information to the output.

I remember back in the bad old .bat days, when running with echo on, individual lines could be exempted by starting them with a @. Is there any equivalent in bash?

#!/bin/bash -x

function i_know_what_this_does() {
  (
    set +x
    echo do stuff
  )
}

echo the next-next line still echoes 'set +x', is that avoidable?
i_know_what_this_does
echo and we are back and echoing is back on

When running the above, output is:

+ echo the next-next line still echoes 'set +x,' is that 'avoidable?'
the next-next line still echoes set +x, is that avoidable?
+ i_know_what_this_does
+ set +x
do stuff
+ echo and we are back and echoing is back on
and we are back and echoing is back on
clacke
  • 633

3 Answers3

27

xtrace output goes to stderr, so you could redirect stderr to /dev/null:

i_know_what_this_does() {
  echo do stuff
} 2> /dev/null

If you still want to see the errors from the commands run inside the functions, you could do

i_know_what_this_does() (
  { set +x; } 2> /dev/null # silently disable xtrace
  echo do stuff
)

Note the use of (...) instead of {...} to provide a local scope for that function via a subshell. bash, since version 4.4 now supports local - like in the Almquist shell to make options local to the function (similar to set -o localoptions in zsh), so you could avoid the subshell by doing:

i_know_what_this_does() {
  { local -; set +x; } 2> /dev/null # silently disable xtrace
  echo do stuff
}

An alternative for bash 4.0 to 4.3 would be to use the $BASH_XTRACEFD variable and have a dedicated file descriptor open on /dev/null for that:

exec 9> /dev/null
set -x
i_know_what_this_does() {
  { local BASH_XTRACEFD=9; } 2> /dev/null # silently disable xtrace
  echo do stuff
}

Since bash lacks the ability to mark a fd with the close-on-exec flag, that has the side effect of leaking that fd to other commands though.

See also this locvar.sh which contains a few functions to implement local scope for variables and functions in POSIX scripts and also provides with trace_fn and untrace_fn functions to make them xtraced or not.

  • Sweet! I was looking to see if there were any modifiers I could apply to the function itself, but I didn't think about simply redirecting stderr. Thanks! – clacke Jan 02 '13 at 09:59
  • 1
    Btw, http://stchaz.free.fr/which_interpreter from the same page is pretty awesome and disturbing. :-) – clacke Jan 14 '13 at 08:38
  • And now I came back here again for the second method, silencing set +x without silencing useful stderr output. Thanks again! – clacke Mar 04 '13 at 12:00
2

The reason that set +x is printed is that set -x means "print the command you are about to run, with expansions, before running it. So the shell doesn't know that you want it to not print things until after it has printed the line telling it not to print things. To the best of my knowledge, there's no way of stopping that from happening.

Jenny D
  • 13,172
0

Here's the solution you've been looking for:

function xtrace() {
  # Print the line as if xtrace was turned on, using perl to filter out
  # the extra colon character and the following "set +x" line.
  (
    set -x
    # Colon is a no-op in bash, so nothing will execute.
    : "$@"
    set +x
  ) 2>&1 | perl -ne 's/^[+] :/+/ and print' 1>&2
  # Execute the original line unmolested
  "$@"
}

The original command executes in the same shell under an identity transformation. Just prior to running, you get a non-recursive xtrace of the arguments. This allows you to xtrace the commands you care about without spamming stederr with duplicate copies of every "echo" command.

# Example
echo "About to do something complicated ..."
xtrace do_something_complicated
  • Or to avoid perl (and problems with multi-line commands): +() { :;} 2> /dev/null; xtrace() { (PS4=; set -x; + "$@";{ set +x; } 2> /dev/null); "$@";} – Stéphane Chazelas Aug 27 '15 at 20:45
  • No, sorry, http://unix.stackexchange.com/a/60049/17980 is the solution I was looking for. :-)

    Do the set -x maneuvers buy me anything compared to just printf >&2 '+ %s\n' "$*"?

    – clacke Sep 01 '15 at 12:13
  • As in: xtrace() { printf >&2 '+ %s\n' "$*"; "$@"; } – clacke Sep 01 '15 at 12:15