0

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?

1 Answer 1

5

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:

!-3

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

!2365

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
    list
-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.

2
  • What does history -a and history -r do exactly?
    – RNA
    Oct 16, 2014 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, 2014 at 11:26

You must log in to answer this question.

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