Take the 2-minute tour ×

starting bash with -v option produces a long output to the console

$ bash -v

source ~/Dropbox/bin/tim_functions.sh

#!/bin/bash ...several hundred more lines

I would like to capture the output to a file to make it easier to browse through, but I have tried bash -v 2>&1 > out_bash.txt and bash -v | tee out_bash.txt and cannot capture the information on the terminal screen within a file. It is as if the verbose output is neither stderr or stdout. How can this be?

Can anyone suggest a way to capture the output of bash -v ?

share|improve this question
add comment

6 Answers

I poked around and found this http://www.commandlinefu.com/commands/view/3310/run-a-bash-script-in-debug-mode-show-output-and-save-it-on-a-file

On the website they use bash -x test.sh 2>&1 | tee out.test, but I tested it with bash -v test.sh 2>&1 | tee out.test and it worked fine.

share|improve this answer
I am doing something a little different though--I am trying to run only 'bash -v' not 'bash -v some_script.sh'. Running bash -v gives the debug information from creating a new shell child process rather than creating a new process running a script. It seems that the debug information is hard to catch in my case. But I did find a way--through using the screen command (see comment below). Thanks for your help. –  Tim Jun 22 '12 at 6:59
add comment
 bash -v 2>&1 > out_bash.txt

is not what you want, it should be

 bash -v  >out_bash.txt 2>&1
share|improve this answer
add comment
up vote 1 down vote accepted

After reading other helpful answers, I believe this issue has to do with how bash is sending the verbose information to tty--which is somehow different than stderr or stdout. It can be caught with the following work around:

$ screen -L 
$ bash -v 
$ exit #from the bash session
$ exit #from the screen session

This results in a screenlog.0 file being generated containing the output.

The bash -v output of interest was on a mac running 10.7.3 (Lion) with

$ bash --version
GNU bash, version 3.2.48(1)-release (x86_64-apple-darwin11)
Copyright (C) 2007 Free Software Foundation, Inc.)

Another 10.6.8 mac I tried had a less (interesting/verbose) output, despite a similar .bashrc file.

share|improve this answer
add comment

you can also use the exec command in the script to redirect all output:

exec >> out.txt 2>> out.txt
set -x
set -v
echo "testing debug of shell scripts"
share|improve this answer
add comment

Have you tried wrapping your child bash in a subshell?

( bash -v ) 2>&1 > out_bash.txt
share|improve this answer
I tried your suggestion, but no luck--still doesn't capture the information. It seems that the output of bash -v is related to debug information. And debug information seems to be neither stderr or stdout. I am starting to think it cannot be rerouted from the terminal. –  Tim Jun 22 '12 at 5:53
Bash is not magical; anything sent to your tty came out of the write() call (or equivalent) from somewhere. What may be happening is the interactive shell itself is producing the output, while redirection may only apply to spawned child processes. This is why I thought wrapping a layer and hoisting the redirect up might work.. –  phs Jun 22 '12 at 5:57
But when you are in a bash session and execute bash -v, that does generate a child process (the new bash session). So unless the parent process was making the write() calls for the debug information of the child, rather than the child process calling write() itself, it still seems like the terminal info should be re-directable. But maybe that is how debug information is generated--the parent somehow monitors the child's activity and reports on it. –  Tim Jun 22 '12 at 6:25
$ screen -L then bash -v command within the screen session, then terminate the bash session with exit and the screen session with exit results in a screenlog.0 file being generated which has the output. Thank you phs for your help. –  Tim Jun 22 '12 at 6:46
@Tim: You should post your solution as an answer and mark it as accepted. –  Dennis Williamson Jun 22 '12 at 10:57
add comment

You can use, bash -v 2>&1 | tee file.txt or bash -v 2>&1 | grep search_string

share|improve this answer
add comment

Your Answer


