1

I want to have a snapshot of all the commands I executed and output (stdout, stderr) in a file to view later. Often I need to view it for debugging purposes.

This link gives the output for single command: How to redirect output to a file and stdout

However, I do not want to write it for each command since it is time consuming. Also, this does not log the command I executed.

Example functionality:

bash> start-logging logger.txt
bash> echo "Hi"
Hi
bash> cat 1.txt
Contents of 1.txt
bash> stop-logging

Contents of logger.txt

bash> echo "Hi"
Hi
bash> cat 1.txt
Contents of 1.txt

Ideally, I want the above behavior. Any other command with some missing functionality (maybe missing command executed from tmp.txt) would also help.

4
  • 1
    Why does bash history not store the commands ? Commented Apr 21, 2016 at 8:44
  • Also the question you linked only saves the output not the command that has been typed Commented Apr 21, 2016 at 8:53
  • @123 Maybe OP are thinking about the local .bash_history and are not the remote? Commented Apr 21, 2016 at 9:13
  • I am sorry .bash_history stores the command on exit which was why I was not able to find in .bash_history. I will update the question. Is there anyway I can get the output of commands executed? Commented Apr 21, 2016 at 9:28

1 Answer 1

1

You can use

$ bash | tee log

to start logging and then

$ ^D

(that's Ctrl-D)

to stop logging.

Create a new bash session that will log, then terminate it when it's not needed.

But unfortunately, bash seems to not write its prompt and commands to stderr so you will only catch command output.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.