Command History
The Mysterious ^[[A
First, start a new Shell /bin/sh
.
Then enter the following command, everything is normal.
Press the up arrow key ⬆️ to view the previous command.
You will find that you cannot see the previous command, instead, these characters ^[[A
appear.
This is because early sh did not support viewing command history, until bash added this feature.
ANSI Escape Sequences
^[[A
is an ANSI escape sequence, indicating the up arrow key.
^[
:represents theEsc
key (ASCII code 27), which is the start of the escape sequence.[A
:represents the specific operation,[A
indicates the up arrow key.
When you press the up arrow key in the terminal, the terminal sends this character sequence ^[[A
to the program,
usually used to call the previous command in the command history.
Since sh does not support viewing command history, it will output the character sequence of the key directly.
sh in Ubuntu
In Ubuntu, sh is actually a link to dash.
Therefore, dash also does not support viewing command history.
History Functionality
history Command
Without arguments, it outputs all history records.
You can also use the short command, with the same effect.
.bash_history File
The persistent file for command history records.
During the interaction with the Shell, the history records of the commands are saved in memory.
Using the history
command can view the changes of the history records in real-time,
but the history records are only written to the .bash_history
file when you exit the Shell.
If you want to write to the history record file immediately, you can use the following command.
Environment Variables
HISTFILE
The path of the history record file.
HISTFILESIZE
The size of the history record file.
HISTSIZE
The size of the history record list in memory.