QA
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→

More Linux Commands & SysAdmin Interview Questions

Beginner LevelLinux Commands & SysAdmin

How do Linux File Permissions (chmod, chown, chgrp) work in Octal and Symbolic notation?

Beginner LevelLinux Commands & SysAdmin

What is the Linux Filesystem Hierarchy Standard (/bin, /etc, /var, /usr, /opt, /tmp)?

Beginner LevelLinux Commands & SysAdmin

What are File Descriptors (0=stdin, 1=stdout, 2=stderr) and Redirection (>, >>, 2>&1)?