How to record the output of a bash script from terminal without suppressing the terminal output like $ ./bash-script.sh >> terminal.txt
? and I want to record the resulting output to text and keep it in the terminal as well.
3 Answers
I want to record the resulting output to text and keep it in the terminal as well
What you want is tee
command. It allows echoing text to stdout
and to a file. For example:
$ ls -l /etc/passwd | tee output_file.txt
-rw-r--r-- 1 root root 2989 6月 17 20:45 /etc/passwd
$ cat output_file.txt
-rw-r--r-- 1 root root 2989 6月 17 20:45 /etc/passwd
-
2The requirement of the question were changing and it seems I was late to adjust the answer according to the last change. congrats! :)– AnwarSep 7, 2016 at 5:28
-
@Anwar indeed , I reviewed the edit history just now. OP clearly kept changing it. Perhaps they didn't phrase it properly right away. Sep 7, 2016 at 5:31
-
Sorry Anwar, i knew the previous hacks, so I thought everyone will answer excluding those hacks, my bad, no one is a mind reader. but thanks for your concern. Sep 7, 2016 at 5:31
-
-
1If you want to capture
stderr
output as well make thatsome_command |& tee output_file.txt
. I think|&
is a csh-ism, but it works in modern bash and is easier than the2>&1
thing. Sep 7, 2016 at 11:00
The tee
command is good for capturing output from non-interactive commands. For interactive terminal applications, the package bsdutils
gives you script
command that allows recording the output to terminal while allowing you to interact with the application like usual. The difference between the two is that script
will give the application the impression of running under a terminal and this might make a difference how the utility behaves. Also, script
captures standard error without requiring to do additional redirects in the shell command line.
You can use it like this:
$ script -c script.sh output.log
(where script.sh
is assumed to be found in $PATH
) or
$ script -c "script.sh arguments" output.log
Running it without -c "${command}"
option will run shell and allows saving the shell session to a file.
As bsdutils
is an "essential" package and its priority is "required" you might have it already installed. Just try issuing command script
(followed by exit
to stop recording to the default file typescript
).
In general I personally usually prefer script
over tee
though many interactive terminal applications seem to run just fine when output is piped to tee
.
-
@PavelSayekat as far as I know, that package comes with Ubuntu by default, so you should already have
script
command installed. The name of the package is exactly that -bsdutils
. Myapt-cache search bsdutils
finds it alright, so maybe you did a typo or something , but it's definitely there , and it's part ofmain
repository Sep 7, 2016 at 9:03 -
script
is a very useful tool, particularly in combination withansifilter
or the like. Just be careful when using it with tools like tmux or GNU screen. In those cases, start script first and don't do anything fancy with the multiplexer.– kojiroSep 7, 2016 at 11:00 -
@Serg yep, I have noticed that too, it is preinstalled in my system, but the typo was from the answer poster :) Sep 7, 2016 at 17:38
-
@FooF when I run like
$ script -c test.sh output.log
, the command does not recognizes my test.sh script, but the test.sh script runs good on its own when I run it, :) Sep 8, 2016 at 5:23 -
1@PavelSayekat This is because
test.sh
is probably not located in any of the directories listed in$PATH
variable.-c
will accept any command that is located in any directories listed in$PATH
. To run script that is in your current directory, you need to either doscript -c ./test.sh output.log
or ` script -c "bash test.sh" output.log` . Possible other issue is that you did not make script executable , so usechmod +x test.sh
Sep 8, 2016 at 5:56
You can use tee
For example,
./script.sh | tee logfile
will cause the output of the script to be saved in logfile
as well as shown in the terminal output.
If you want to store all outputs of subsequent script executions, you may want to append to that file. In that case, you'd use tee -a
instead
./script.sh | tee -a logfile
./script2.sh | tee -a logfile
How do I save the output of a command to a file?
This question asksI want to record the resulting output to text and keep it in the terminal as well.
While the linked post provides partial answer, this is not exact duplicate, hence reopening this question