I have an incremental build command for my project that will output something if I do a clean build, and output something else (less) if I do an incremental build.

How can I save this if I already run a clean build mycommand.sh and the output is printed on terminal window (without using copy and paste)?

I forgot to do

mycommand.sh > myfile.txt


mycommand.sh >> myfile.txt

for saving the output to a file.

  • 1
    You can use $(!!) to recompute (not re-use) the output of the last command. Commented Apr 12, 2020 at 16:37
  • 2
    This might help: man script
    – Cyrus
    Commented Apr 12, 2020 at 21:07

2 Answers 2


While $(!!) or rerunning the previous command might seem (or sometimes be) a solution, it also has its drawbacks you have to fully understand using it.

Example: If the command did some modifications like

rm -rv /tmp/*

will do, then rerunning that command in any way can not produce the same output as before with the exception that if /tmp/ was empty before the first run and it is still empty the second time, the output should be the same.

Bottom line: There is no safe way to reuse or save (without copy&paste) the output of the last command if you didn't save it while the command was executed.

  • Thank you for your addition. You are correct in your analysis. Commented Apr 12, 2020 at 21:23

You can't since bash doesn't allocate any output to any parameter or any block on its memory.

However, you can use $(!!) to recompute (not re-use) the output of the last command.

The !! on its own executes the last command.

$ echo pierre
$ echo my name is $(!!)
echo my name is $(echo pierre)
my name is pierre

Source: Reusing output from last command in Bash

  • This might have some implications!! $ echo $(date) So 12. Apr 22:42:31 CEST 2020 $ echo the date was $(!!) echo the date was $(echo $(date)) the date was So 12. Apr 22:42:59 CEST 2020 where the output is not the same as before!!
    – bey0nd
    Commented Apr 12, 2020 at 20:43
  • Sorry, can't get the line-breaks to work. The $(!!) reruns the command and doesn't 'only' recompute the output. Therefore if the previous run modified some counters or did something based on time, the output of the rerun might not be the same as the first time!!
    – bey0nd
    Commented Apr 12, 2020 at 20:54
  • Sorry, I hesitated to down vote, but after reading through the link you provided and realizing that you 'just' copied the content of that link, without any comment regarding the problems of rerunning a command, I had to. Example: rerunning sudo rm -rv /tmp/* will not 'recompute' the output of the first run!!
    – bey0nd
    Commented Apr 12, 2020 at 21:10

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .