To see the last command you executed in Linux, use the built-in command history feature. No extra setup is required—just run a simple command in your terminal. The history is stored in a file (usually ~/.bash_history for Bash) and can be viewed interactively or programmatically. For persistent access, configure your shell to display history automatically when you open a terminal.

What You'll Need

  • The Linux terminal (default on most Linux distros; available via Ctrl+Alt+T in GNOME/KDE or Terminal app in others).
  • A shell that supports command history (Bash, Zsh, Fish, etc.). Most modern Linux systems use Bash by default.
  • Basic terminal knowledge (open a shell, type commands, and press Enter).

Step-by-Step

  1. Open your terminal. Press Ctrl+Alt+T (GNOME/KDE) or launch the Terminal app (Xfce, LXQt, etc.). If you’re unsure which shell you use, run echo $SHELL to check (common shells: Bash, Zsh, Fish).
  2. View the last command interactively. Run one of these commands to display your most recent history:
    • history: Lists all commands in reverse chronological order (last command first). Press q to exit.
    • fc -l: Shows a numbered list of your last commands (e.g., fc -l 10 to see the last 10).
    • history | head -n 1: Displays only the last entry.
  3. Access the last command directly. Use the shell’s history navigation:
    • !!: Repeats the last command entered.
    • !# (e.g., !10): Repeats the command entered at position #10 in the history.
    • Ctrl+R: Press repeatedly to search and rerun commands from history (type a partial command to filter).
  4. View history in a file (advanced). The full history is saved in ~/.bash_history (create it if missing with touch ~/.bash_history). To edit it:
    1. Open the file with a text editor: nano ~/.bash_history (type Ctrl+O to save, Ctrl+X to exit).
    2. Look for your last command near the top of the file.

Common Mistakes

  • Shell doesn’t recognize history commands. If history or !! fail, your shell may not be recording history. Check if history is enabled by running: echo $HISTFILESIZE If it returns 0 or lacks a value, history is disabled. Enable it in your shell config file (e.g., ~/.bashrc or ~/.zshrc) by adding: HISTFILESIZE=10000 HISTSIZE=10000 Then reload with source ~/.bashrc or restart the terminal.
  • History shows old commands. If you’re unsure what’s in history, clear it with: history -c (clears the current session) or rm ~/.bash_history (clears permanent history).
  • Permissions issue. If you can’t read/write ~/.bash_history, add your user to the wheel group (Linux) or use sudo to edit: sudo nano /root/.bash_history

Tips

  • Make history persistent. To see history every time you open a terminal, add this to your shell config file: if [ -f ~/.bash_history ]; then source ~/.bash_history; fi This ensures history loads automatically.
  • Filter history. Use grep to search for commands: history | grep "command" Example: history | grep "ls" lists all ls commands.
  • Save history to a file. Redirect history to a file for later review: history > my_history.txt
  • Use a different shell. If Bash isn’t your default, switch to Zsh (Ubuntu/Debian): sudo apt install zsh chsh -s $(which zsh) Then follow the initial setup prompts.

Troubleshooting

If the last command isn’t visible, try these steps:

  1. Restart your shell. Close and reopen the terminal. If you’re in an interactive session (e.g., SSH), restart with exec bash.
  2. Check shell settings. Run: echo $HISTSIZE $HISTFILESIZE If these values are low, increase them in your shell config (e.g., HISTSIZE=10000).
  3. Verify history file exists. Run: ls -la ~/.bash_history If it’s missing, create it: touch ~/.bash_history Then add commands to see it populate.
  4. Test with a minimal setup. Open a new terminal and type a command manually. If history appears, the issue is with your existing session.
  5. Reinstall the shell. If nothing works, reinstall Bash: sudo apt reinstall bash (For Debian/Ubuntu) or sudo dnf reinstall bash (For Fedora/RHEL).

Frequently Asked Questions

Why isn’t my last command showing up in history?

History may be disabled or empty if you haven’t run any commands since starting the session. Enable it permanently by adding HISTSIZE=10000 to your shell config file.

How do I clear the history without deleting my commands permanently?

Use history -c to clear the current session’s history or rm ~/.bash_history to clear it permanently.

Can I see the last command in a non-interactive shell (e.g., scripts)?

Yes, but history won’t be available. To capture the last command in a script, use echo $BASH_COMMAND or echo $LAST_COMMAND.

What if my shell isn’t Bash (e.g., Zsh, Fish)?

Zsh uses history similarly but may have additional options like fc -l. Fish stores history in ~/.config/fish/fish_history.

How do I prevent sensitive commands from appearing in history?

Use a command alias with set +x to hide output while typing: alias mycmd="set +x; command mycmd" Then type mycmd instead of command mycmd.

Can I search for a specific command in my history?

Use history | grep "search_term" or fc -l | grep "search_term" to filter results.