Wednesday, January 18, 2017

Linux: Using Command History

The bash shell supports command history. Every time you enter a command at the shell prompt, that command is saved in the ~/.bash_history file in your home directory. This file is just a simple (hidden) text file that contains all of your previously entered shell commands, one on each line. This file is continually updated each time you enter a shell command. You can display the contents of the .bash_history file by entering history at the shell prompt.
If you press the UP ARROW key at the shell prompt, bash will read this file and display the last command you entered. If you press the UP ARROW key repeatedly, you can scroll through a list of your last-used commands. When you arrive at the one you want, simply press ENTER to execute the command. I love this feature, especially if I need to retype a very long, complex command. Just hit the UP ARROW key until the desired command is displayed and then press ENTER!
If you don’t want to arrow through all of your past commands to find the one you want, you can also enter a part of the command you need and then press CTRL-R. The bash shell will search through your command history and display the most recent matching command.
You can manage the entries in your history file using the following environment variables:
     HISTSIZE or HISTFILESIZE   Configures the size of your history file. On most distributions, this is set to 1,000 entries. You can customize the size of your history file by changing the value of this variable.
     HISTCONTROL   Controls how your command history is stored. You can set this variable to a value of ignoredups, ignorespace, ignoreboth, or erasedups. A value of ignorespace tells the shell to ignore commands in the history that start with spaces. A value of ignoredups tells the shell to ignore duplicate commands in the history. A value of ignoreboth specifies both ignorespace and ignoredups. You can also set this variable to a value of erasedups to remove all duplicate entries in the history file.

No comments:

Post a Comment