There are two terminal window - terminal A and terminal B.

I typed a long long command to do something on A.

then I want to execute the same command on B.

But I would NOT like to type it again nor drag-copy-paste since it's very very long.

What's the best(fastest) way to do so?


Write out your history in the source terminal:

history -a

Then load it in your target terminal:

history -r

If you executed nothing in between, the long long command should be the third last command:


Or you can do history, note the number of the long long command (say 2365), and do:


From help history:

-a  append history lines from this session to the history file
-r  read the history file and append the contents to the history
-w  write the current history to the history file
    and append them to the history list

You can also use history -n instead of -r:

-n  read all history lines not already read from the history file

Also see this SO question.

| improve this answer | |
  • What does history -a and history -r do exactly? – RNA Oct 16 '14 at 11:15
  • 1
    @RNA -a appends the current history to the history file (usually ~/.bash_history), and -r reads in the history file and appends it to the current history. – muru Oct 16 '14 at 11:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.