Friday, May 10, 2024
10
rated 0 times [  10] [ 0]  / answers: 1 / hits: 2823  / 1 Year ago, sat, april 29, 2023, 4:02:47

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?


More From » command-line

 Answers
2

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



[#28319] Monday, May 1, 2023, 1 Year  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
erettor

Total Points: 303
Total Questions: 121
Total Answers: 103

Location: Colombia
Member since Mon, May 2, 2022
2 Years ago
;