1

Personally I prefer the Windows way of command history traversal (sorry!) where each command retains its place in the history and when you return to the last-executed command it's not necessarily last in the history.

Not sure if that makes sense so I will try to explain with examples.

Bash

Suppose I executed four commands (one, two, three, four). After this, the command history (from earliest to most recent) looks like:

  • one
  • two
  • three
  • four

Now if I press up three times, I will go back to four, then three, then two as expected. If I press enter, I will execute command two. Now the history looks like:

  • one
  • two
  • three
  • four
  • two

If I press I will see two. If I press it again, I will see four and so on.

Windows

Suppose I executed the same four commands (one, two, three, four). After this, the command history (from earliest to most recent) looks like:

  • one
  • two
  • three
  • four

Now if I press up three times, I will go back to four, then three, then two as before. If I press enter, I will execute command two. Now pressing again, it will still show two. But the history still looks like:

  • one
  • two
  • three
  • four

What this means is that I have "jumped" back to an earlier point in the history. I can now press to see one, or to see three, etc.

Why this is useful

This is particularly useful when you have just run a series of commands and you want to run them again. It involves significantly less keystrokes to run the whole series again.

Is there a way to cause bash to behave the same way?

2

This doesn't directly answer your question, but it may be helpful if there ends up being no true answer.

If you've executed some sequence of commands:

$ one
$ two
$ three
$ four

$ history
1  one
2  two
3  thee
4  four

and you want to execute the sequence again, you could do the following:

!1; !2; !3; !4

Or, if you want to execute subsequent commands only if the earlier ones succeeded:

!1 && !2 && !3 && !4

Then, you'll get an entry like that in your history:

$ history
1  one
2  two
3  three
4  four
5  one; two; three; four
6  one && two && three && four

Then, if you want to re-execute that sequence again, you can use the arrows or just use, for instance, !5

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.