History is a list of past lines
Bash keeps a list of the lines you enter in an interactive session. The list lives in memory while the shell runs, and it is saved to a file, usually .bash_history in your home directory, when the session ends. The variables HISTSIZE and HISTFILESIZE set how many lines are kept in memory and in the file. The history builtin prints the list with numbers, which you can reuse with an exclamation mark followed by the number.
History is an interactive feature. A script run with bash does not record its commands, because the history is turned on only for interactive shells. That is why the lab does not show history in a checked exercise: the checks run scripts, and the history of a script is empty.
History expansion
History expansion is a set of shortcuts that rewrite a line before it runs. The double exclamation mark repeats the last command, and the exclamation mark followed by a string repeats the most recent command that starts with that string. The word designator !$ means the last argument of the previous command, which is useful after a mkdir or a cp. Expansion happens before quoting is removed, so single-quoted text is left alone.
mkdir -p reports/2026
cd !$Run these as two separate commands. The !$ in the second line expands to the last argument of the previous history entry, which is reports/2026. History expansion works on earlier lines, not on an earlier command within the same line, so mkdir -p reports/2026 && cd !$ would pick up the last argument of whatever you ran before it. Bash prints the expanded line before running it, so you can see what happened.
Searching history
Ctrl-r starts a reverse incremental search. You type part of a command, and readline finds the most recent line that contains it. Press Ctrl-r again to go further back, and press Enter to run the match or an arrow key to edit it. The search is faster than scrolling, and it works across sessions once the history file is saved.
The history builtin also accepts options. history -a appends the lines of the current session to the file now, which is useful when you run several terminals and want each one to see the others.
Readline names and bindings
Readline is a library, and every editing action has a name, such as beginning-of-line, end-of-line, and history-search-backward. A key is bound to a name, and the bindings are shown by bind -p, which prints them in a form that can be saved. bind -l lists the names of all readline functions, and bind -P shows which keys trigger each one.
bind -l | grep historyThe control key notation in readline is Ctrl followed by a hyphen. Ctrl-a is beginning-of-line and Ctrl-e is end-of-line, which are the two most useful motions on a command line. Ctrl-u deletes from the cursor to the start of the line, and Ctrl-k deletes to the end.
inputrc: saving your choices
Readline reads its settings from a file named inputrc, usually in your home directory. A line in the file either sets a variable, such as editing-mode, or binds a key sequence to a function or a macro. The bind -f command reads a file on demand, which is handy for testing a binding without restarting the shell.
bind -f my-inputrcA macro binding has a quoted key sequence on the left and a quoted string on the right, and the string is typed as if you had entered it. Escape sequences such as backslash C followed by a hyphen mean Ctrl, and backslash n means Enter.
Set editing-mode to vi in inputrc if you prefer vim-style editing of the command line. The change applies to readline and not to the shell language, so your scripts are unaffected.
Practice on the lab machine
The lab shell is not interactive during a checked step, so the exercises read the binding tables and load a small inputrc file with bind -f. Use the help builtin to read the bind command, and the online bash manual for the full list of readline names and variables.
Expansion that avoids retyping
Some expansions take the words of an earlier command. The sequence !:0 is the command name, and !:2 is the second word. The caret sign followed by a pattern replaces the first match in the previous command, which helps fix a typo in a long path without retyping the whole line. These expansions are printed before the line runs, so an unexpected substitution shows up on the screen rather than in a file.
Keeping history useful
Commands that start with a space are not saved when the variable HISTCONTROL includes ignorespace, which is a common way to keep a password or a token out of the history file. Duplicate lines can be removed with ignoredups or ignoreboth. These settings belong in your startup file, so they apply to every interactive shell you open.
Searching by prefix
Readline has a prefix search that works on the text before the cursor. Binding a key to history-search-backward makes the up arrow find only the lines that start with what you have typed, which is faster than scrolling through every recent command. The function names are the same as in bind -l, so a binding can be tested with bind -f before it is made permanent in the startup file.
Vi and emacs modes
Readline has two editing modes. Emacs mode is the default, with the control key motions described above. Vi mode gives you normal and insert modes on the command line, similar to vim. Switching modes changes how the keys behave, but it does not change how the shell parses a line, so a switch never changes what a command does.