Redirect to file, including current prompt line

io-redirection

How to include the redirected command itself to the output file redirection? For example

echo "hello!">output.txt

I want the content of output file like this:

echo "hello!">output.txt
hello!

Best Answer

You may want one of two things here.

  1. Using tee to get the result of the echo sent to the terminal as well as to the file:

    $ echo 'hello!' | tee output
    hello!
    $ cat output
    hello!
    
  2. Using script to capture the whole terminal session:

    $ script
    Script started, output file is typescript
    $ echo 'hello!' >output
    $ cat output
    hello!
    $ exit
    Script done, output file is typescript
    
    $ cat typescript
    Script started on Sat Nov 10 15:15:06 2018
    $ echo 'hello!' >output
    $ cat output
    hello!
    $ exit
    
    Script done on Sat Nov 10 15:15:37 2018
    

If this is not what you are asking for, then please clarify your question.

Related Question