This question already has an answer here:

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.

marked as duplicate by Sergiy Kolodyazhnyy command-line Sep 8 '16 at 7:22

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • It simply writes any output into a file. The language is irrelevant, not sure what you mean? – Jacob Vlijm Sep 7 '16 at 7:15
  • 4
    In what way? it is an exact dupe, what requirements? – Jacob Vlijm Sep 7 '16 at 7:59
  • The alleged duplicate asks How do I save the output of a command to a file? This question asks I 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 – Sergiy Kolodyazhnyy Sep 8 '16 at 5:52
  • @Serg Common Serg, Why? Let's not do this. You know there are many other candidates, it is a dupe anyway and the dupe fully addressed his issue. Nothing wrong with posting a dupe. – Jacob Vlijm Sep 8 '16 at 6:57
  • @user72216 I am sure it's a dupe, but not of the one that was linked. I did find a different one. Will close it shortly – Sergiy Kolodyazhnyy Sep 8 '16 at 7:22

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. My apt-cache search bsdutils finds it alright, so maybe you did a typo or something , but it's definitely there , and it's part of main repository – Sergiy Kolodyazhnyy Sep 7 '16 at 9:03
  • script is a very useful tool, particularly in combination with ansifilter 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. – kojiro Sep 7 '16 at 11:00
  • @Serg yep, I have noticed that too, it is preinstalled in my system, but the typo was from the answer poster :) – Pavel Sayekat Sep 7 '16 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, :) – Pavel Sayekat Sep 8 '16 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 do script -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 use chmod +x test.sh – Sergiy Kolodyazhnyy Sep 8 '16 at 5:56

 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
  • 2
    The requirement of the question were changing and it seems I was late to adjust the answer according to the last change. congrats! :) – Anwar Sep 7 '16 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. – Sergiy Kolodyazhnyy Sep 7 '16 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. – Pavel Sayekat Sep 7 '16 at 5:31
  • @PavelSayekat no problem. That's ok :) – Anwar Sep 7 '16 at 5:32
  • 1
    If you want to capture stderr output as well make that some_command |& tee output_file.txt. I think |& is a csh-ism, but it works in modern bash and is easier than the 2>&1 thing. – Tor Klingberg Sep 7 '16 at 11:00

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

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