2

From the description of set in the bash man page:

-v   Print shell input lines as they are read.

Thus, the following example script:

#!/usr/bin/env bash
# setv.sh
set -v
foo=bar
echo $foo

Generates output:

foo=bar
echo $foo
bar

Is there a way for it to prepend some string, say "+ " to each line, so as to clearly indicate which line is a line from the script, and which line is output of a line of from the script. Using the above example, the desired output would be:

+ foo=bar
+ echo $foo
bar
  • What do you mean when you say "The output looks like"? What command did you execute to generate that undesirable output? – user1404316 Mar 15 '18 at 20:11
  • I think I now understand what you are looking for. I'll try to edit your question to make it clearer. – user1404316 Mar 15 '18 at 20:21
3

To get that kind of output you could use $BASH_COMMAND e.g. add

trap 'printf "%s %s\n" + "$BASH_COMMAND" >&2' DEBUG

instead of set -x/v at the top your script.

  • You would probably want to print that on stderr. The OP might also want set -o functrace – Stéphane Chazelas Mar 15 '18 at 21:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.