0
0
Linux CLIscripting~10 mins

History command and search in Linux CLI - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - History command and search
User runs commands
Commands saved in history list
User types 'history'
Display all past commands
User types 'history | grep keyword'
Filter and show matching commands
User types '!number' or '!keyword'
Execute command from history
Commands run are saved in history. 'history' shows all. You can search with grep or run past commands by number or keyword.
Execution Sample
Linux CLI
history
history | grep ls
!3
!ls
Shows all commands, filters commands with 'ls', runs command number 3, runs last command starting with 'ls'.
Execution Table
StepInput CommandActionOutput/Result
1historyShow all saved commands1 ls -l 2 pwd 3 echo Hello 4 ls -a
2history | grep lsFilter commands containing 'ls'1 ls -l 4 ls -a
3!3Run command number 3 from historyHello
4!lsRun last command starting with 'ls'List of files including hidden ones
5exitExit shell
💡 User exits shell, stopping history command usage
Variable Tracker
VariableStartAfter 1After 2After 3After 4Final
history_listempty['ls -l']['ls -l', 'pwd']['ls -l', 'pwd', 'echo Hello']['ls -l', 'pwd', 'echo Hello', 'ls -a']['ls -l', 'pwd', 'echo Hello', 'ls -a']
last_command_executednonels -lpwdecho Hellols -als -a
Key Moments - 3 Insights
Why does '!3' run 'echo Hello' and not 'ls -a'?
Because '!3' runs the command with history number 3, which is 'echo Hello' as shown in execution_table step 3.
What happens if I type '!ls' but no command starts with 'ls'?
The shell will show an error saying no such command in history, because '!ls' runs the last command starting with 'ls' only if it exists.
Does 'history | grep ls' change the history list?
No, it only filters and shows commands containing 'ls' but does not modify the saved history list, as seen in execution_table step 2.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what command does '!3' run?
Aecho Hello
Bls -a
Cpwd
Dls -l
💡 Hint
Check step 3 in the execution_table where '!3' is run.
At which step does the history command filter commands containing 'ls'?
AStep 1
BStep 2
CStep 3
DStep 4
💡 Hint
Look at the 'Input Command' column for 'history | grep ls'.
If a new command 'ls -R' is run after step 4, what will '!ls' run next?
Als -a
Bls -R
Cecho Hello
Dpwd
💡 Hint
Consider that '!ls' runs the last command starting with 'ls' from the updated history_list.
Concept Snapshot
history: shows all past commands with numbers
history | grep keyword: filters commands containing keyword
!number: runs command by its history number
!keyword: runs last command starting with keyword
Commands are saved in order as you run them
Full Transcript
The history command in Linux saves all commands you run. Typing 'history' shows the list with numbers. You can search this list using 'history | grep keyword' to find commands containing that word. To run a past command, type '!number' to run the command with that number, or '!keyword' to run the last command starting with that keyword. This helps you quickly repeat or find commands without retyping them. The execution table shows how commands are saved, searched, and run step-by-step.