Beginner LevelLinux Commands & SysAdmin
How does shell history work (history, !$, !n, Ctrl+R)?
❓ Interview Prompt: Explain Bash history file (~/.bash_history), history command, interactive reverse search (Ctrl+R), !! (last command), and !$ (last argument).
Bash records executed shell commands in memory and saves them to '~/.bash_history' upon exit:
History Shortcuts:
•
'Ctrl+R': Interactive reverse-i-search to locate past commands by typing keywords.
•
'!!' ('bang bang'): Re-executes the exact previous command (e.g. 'sudo !!').
•
'!$': Evaluates to the last argument of the previous command.
•
'!105': Re-executes command line #105 from history list.
Code Implementation Example
bash
# Forgot sudo on previous command? Use bang-bang:
sudo !!
# Make directory and navigate inside using last argument shortcut (!$):
mkdir -p /var/www/app/releases/v1.0.0
cd !$💡Re-executing commands with sudo !! and navigating via !$ argument shortcuts.
🎯 Interview Key Takeaway
Use Ctrl+R to search past command history interactively, and sudo !! to re-run the last command with root privileges.
#History#Bash#Shortcuts#Ctrl+R#Beginner
Back to all Linux Commands & SysAdmin Questions→