10

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.

Sergiy Kolodyazhnyy
  • 105,154
  • 20
  • 279
  • 497
  • It simply writes any output into a file. The language is irrelevant, not sure what you mean? – Jacob Vlijm Sep 07 '16 at 07:15
  • 4
    In what way? it is an exact dupe, what requirements? – Jacob Vlijm Sep 07 '16 at 07: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 08 '16 at 05: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 08 '16 at 06: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 08 '16 at 07:22
  • I think this kind of functionality cannot happen by design since that would eat up disk space like sweet cookies. What looks possible to me is to use some Log feature implemented in the commands that have it available. – userDepth Sep 15 '16 at 01:28
  • @userDepth, The last log is important, if the terminal is set to record last 3 days log only of it's operations, will that hurt? – Pavel Sayekat Oct 18 '16 at 04:33
  • @PavelSayekat That should be done by ROOT uknow. And root should change it's password regularly or other alternative for security hardening. Three days may trump using snapshots but would take more space. am I right ? – userDepth Oct 21 '16 at 14:47
  • Though the question is not exactly the same but the answers has my solution here https://askubuntu.com/questions/420981/how-do-i-save-terminal-output-to-a-file/731237#731237. It took long for me to get which one is the exact. – Pavel Sayekat Sep 15 '17 at 21:26

3 Answers3

10

 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
Sergiy Kolodyazhnyy
  • 105,154
  • 20
  • 279
  • 497
  • 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 07 '16 at 05: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 07 '16 at 05: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 07 '16 at 05:31
  • @PavelSayekat no problem. That's ok :) – Anwar Sep 07 '16 at 05: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 07 '16 at 11:00
  • It's the same thing. If ‘|&’ is used, command1’s standard error, in addition to its standard output, is connected to command2’s standard input through the pipe; it is shorthand for 2>&1 | ( https://www.gnu.org/software/bash/manual/bash.html ) Also , &> . But 2>&1 is portable, can be used in zsh and ksh, others are bash specific – Sergiy Kolodyazhnyy Sep 07 '16 at 17:01
  • @Serg that solution makes total sense but somehow it is not working for me, so untill I sort it out........, you know what I mean :) – Pavel Sayekat Sep 08 '16 at 05:20
  • @PavelSayekat Can you explain what exactly is not working ? Are there any errors ? Use paste.ubuntu.com if you want to show any output, just copy text there and provide link here in comments – Sergiy Kolodyazhnyy Sep 08 '16 at 05:50
  • @TorKlingberg yes, |& and 2>&1 both works for scripts also to capture logs to file. – Pavel Sayekat Sep 16 '17 at 14:35
9

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
Anwar
  • 76,649
9

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.

FooF
  • 270
  • @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 07 '16 at 09: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 07 '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 07 '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 08 '16 at 05: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 08 '16 at 05:56
  • @SergiyKolodyazhnyy Yep script -c "bash test.sh" output.log worked. – Pavel Sayekat Sep 15 '17 at 19:46