Bash – Logging bash output to a file

bashcommand linelogspipeshell

I'm trying to build vlc, which is rather complicated and dependent on having the right packages installed, and keeps choking on errors. To trace all my steps, I wish to output what I'm doing to a logfile as I interact with a shell.

As per the helpful answer to this question

I got an elevated prompt by typing the following

(sudo bash) | tee -a vlc_attempt.log

Unfortunately, the output does not include the path prompt, which I wish to see to tell input apart from output.

I tried piping stderr to stdout and vice versa, both of which produce awry results, but enter nothing into the file. Same thing with trying to pipe lxterminal to tee, or starting lxterminal with the –command= option and then the above example command in quotes. Where to go from here?

Best Answer

Use script(1) to log everything sent to the terminal:

$ script
Script started, file is typescript
$ # do your work
...
$ # then exit with ^D
$ exit
Script done, file is typescript

You can later look at the output with less:

$ less -r typescript

Beware that the logs will contain all control characters sent to the terminal, such as ANSI colours or whatever else your shell prompt sends. If you don't want control characters in the logs then either simplify your PS1 before running script, or use something like stripansi(1) to clean up the output.

Related Question