9

This is something that I have wanted to know for a really long time, but never came around to actually posing a question.

I know that in the terminal that is provided in Linux distros, the entire history of commands can be seen by using the arrow keys (if there is any other method, I'm not aware of it), so how does this happen?

Is there someplace where the terminal logs all the keystrokes? Is it some sort of buffer? But, then again, if it was a buffer, it would be cleared out after a while, would it not?

How does this system work?

5
  • write history to get all previously used commands..
    – Sukupa91
    Commented Nov 19, 2013 at 6:21
  • The OP is not asking about the history command but how this whole system of remembering old commands works.
    – Tarun
    Commented Nov 19, 2013 at 6:24
  • sorry i got it wrong..
    – Sukupa91
    Commented Nov 19, 2013 at 6:31
  • 2
    Mind this exception: if the command starts with a space the command will not end up in history.
    – Rinzwind
    Commented Nov 19, 2013 at 11:03
  • @Rinzwind , It does gets added to history, if started with a space.
    – GC 13
    Commented Feb 28, 2014 at 10:41

2 Answers 2

10

This is the command history and it is a feature of the shell rather than the terminal.

On Ubuntu (and a lot if not most of the other Linux distributions) the default shell for interactive use is Bash (/bin/bash). Bash keeps your history, that is a list of the last commands at ~/.bash_history. When you open a shell (usually by opening a terminal) this file is read and loaded into the internal history of this shell. Once you close the shell, the changes are written back to the file. By default this is limited to the last 500 commands.

There are a few variables and shell options that can change the behaviour of how this works exactly. Have a look at the bash manpage (man bash) if you want to know more. Just search for HISTORY (type /^HISTORY and confirm with Enter, jump to next find with n)

Also there are quite a lot more methods to go around the history than just Up and Down:

  • For example, press Ctrl+R and part of a previous command, bash will search backwards in the history for a matching command, repeat pressing Ctrl+R if the first match is not the one you are looking for. (See 'Commands for Manipulating the History' in the bash manpage)

  • Copy /etc/inputrc to ~/.inputrc and uncomment the lines with history-search-forward and history-search-backward in them. If you type the first letters of a command and press PgUp, bash will show only commands from the history that start with the same letters. (Work only in shells opened after this change was made)

  • Type !! instead of a command on the shell, this will repeat the last command (see 'HISTORY EXPANSION' in the bash manpage for more information on that).

1
  • 1
    And if you don't want it to keep a history, put unset HISTFILE into your ~/.bashrc. Then it will only keep history in memory, clearing it for each new shell invocation.
    – user
    Commented Nov 19, 2013 at 10:13
10

Command history is generally stored on per-user basis in the .bash_history file in your home directory. Means, every user has its own set of commands he has executed.

When multiple terminal sessions are open they may show you different commands when pressing the arrow key, but when you will close all the terminal windows the history from different terminal session will be merged to a common ~/.bash_history file.

You must log in to answer this question.

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