How many times have you seen someone try to "Write the command that I run and the output of the command"? This often happens, and for viewing, the command you are working with set -v is good, ( set -x is nice too, but it can be harder to read), but what happens when you want to capture an executable command ... but not all commands run
Launch interactively. I see no way to write the output of set -v .
set -v echo a 1>/dev/null
I can put this in a script and things will get better:
echo 'set -v'$'\n''echo a' > setvtest.sh bash setvtest.sh 1>/dev/null
Aha, so from the script it goes to stderr. What about inline?
set +v { set -v ; echo a ; } 1>/dev/null
Hmm, no luck.
Interestingly, as a side note, this does not yield a solution:
echo 'set -v ; echo a' > setvtest.sh bash setvtest.sh 1>/dev/null
I am not sure why, but, perhaps, therefore, why the subshell version does not return anything.
What about shell functions?
setvtest2 () { set -v echo a } setvtest2
Now the question is: Is there a good way to capture the output of set -v ?
Here is my bad hack, so I'm looking for something less crazy:
#!/usr/bin/env bash script=/tmp/$$.script output=/tmp/$$.out echo 'set -v'$'\n'"$1" >"$script" bash "$script" 1>"$output" cat "$output" rm -f "$script" "$output"
Now I can execute simple scripts
bash gen.sh 'echo a' 1>/dev/null
But of course there are better ways.