Is it possible to set a fixed number for the "history" command to only show a certain amount of history items like 100, instead of everything from the beginning of time?
2 Answers
history n prints only n lines of the history. For example:
$ history 4
2000 type history
2001 help
2002 help history
2003 history 4
$
So we can make an alias in your .bashrc:
alias h="history 100"
From help history:
history: history [-c] [-d offset] [n] or history -anrw [filename] or history -ps arg [arg...]
Display or manipulate the history list.
Display the history list with line numbers, prefixing each modified
entry with a `*'. An argument of N lists only the last N entries.
This is useful if you want to keep a large history set, maybe even an unlimited one. With HISTCONTROL=ignoreboth:erasedups and reverse incremental search, it's a special occasion when you actually run the history command.
Setting HISTFILESIZE and HISTSIZE you can restrict the size of history command.
For example HISTFILESIZE=100 and HISTSIZE=100. It will restrict history file to store 100 lines and HISTFILE which stores your cureent session command in memory to 100 lines.
When the shell starts up, the history is initialized from the file named by the HISTFILE variable (default ~/.bash_history). The file named by the value of HISTFILE is truncated, if necessary, to contain no more than the number of lines specified by the value of the HISTFILESIZE variable.
You can add HISTFILESIZE=100 and HISTSIZE=100 in your ~/.bashrc file . Change the 100 with the number you want.
Explanation
HISTFILESIZE=10 and HISTSIZE=10
- You start your session.
- Your HISTFILE (file that stores your bash command history), is truncated to contain HISTFILESIZE=10 lines.
- You write 50 lines.
- At the end of your 50 commands, only commands 41 to 50 are in your history list, whose size is determined by HISTSIZE=10.
- You end your session.
- Assuming
histappendis not enabled, commands 41 to 50 are saved to your HISTFILE which now has the 10 commands it held at the beginning plus the 10 newly written commands. - Your HISTFILE is truncated to contain HISTFILESIZE=10 lines.
- Assuming
- You now have 10 commands in your history - the last 10 that you just typed in the session you just finished.
- When you start a new session, you start over at 1 with a HISTFILE of HISTFILESIZE=10.
HISTFILESIZE=10 and HISTSIZE=5
- You start your session.
- Your HISTFILE (file that stores your bash command history), is truncated to contain at most HISTFILESIZE=10 lines.
- You write 50 lines.
- At the end of your 50 commands, only commands 46 to 50 are in your history list, whose size is determined by HISTSIZE=5.
- You end your session.
- Assuming
histappendis not enabled, commands 46 to 50 are saved to your HISTFILE which now has the 10 commands it held at the beginning plus the 5 newly written commands. - Your HISTFILE is truncated to contain HISTFILESIZE=10 lines.
- Assuming
- You now have 10 commands in your history - 5 from a previous session and the last 5 that you just typed in the session you just finished.
- When you start a new session, you start over at 1 with a HISTFILE of HISTFILESIZE=10.
HISTFILESIZE=5 and HISTSIZE=10
- You start your session.
- Your HISTFILE (file that stores your bash command history), is truncated to contain at most HISTFILESIZE=5 lines.
- You write 50 lines.
- At the end of your 50 commands, only commands 41 to 50 are in your history list, whose size is determined by HISTSIZE=10.
- You end your session.
- Assuming
histappendis not enabled, commands 41 to 50 are saved to your HISTFILE which now has the 5 commands it held at the beginning plus the 10 newly written commands. - Your HISTFILE is truncated to contain HISTFILESIZE=5 lines.
- Assuming
- You now have 5 commands in your history - the last 5 that you just typed in the session you just finished.
- When you start a new session, you start over at step 1 with a HISTFILE of HISTFILESIZE=5.
"history 100"
I should get a history list that displays 100 commands? This didn't work for me, but I'm probably not following you correctly
– Justin Sep 20 '14 at 18:47historyonly shifts numbers when commands are removed. (For example, if you use theHISTSIZEvariable.) – muru Sep 20 '14 at 19:05HISTSIZEvariable, and notHISTFILESIZE. The former controls how many commands bash keeps in memory, the latter how many bash keeps in the history file. Though I'd advice against such a small limit. Once you get used to incremental reverse search, you'd want all the commands you ever typed in history. :) – muru Sep 20 '14 at 19:18