- Posted on
- • commands
Bash History Hacks: Reusing Previous Commands
- Author
-
-
- User
- Linux Bash
- Posts by this author
- Posts by this author
-
Bash History Hacks: Mastering the Art of Reusing Previous Commands
Bash, the born-again shell, is an integral part of a Linux and Unix user's toolbox. Once you've become acquainted with its intricacies, it offers unforeseen efficiencies in day-to-day tasks. One of its most powerful and least exploited features is the history system, which, when utilized adeptly, can drastically streamline workflows. Leveraging the bash history effectively can transform a cumbersome revisit of earlier tasks into a swift stroke of command line magic. Let's dive into some practical hacks to turn you into a bash history wizard.
Understand the Basics
Before we hack away, it's crucial to grasp how bash history works. Bash automatically logs commands you enter into a file, typically .bash_history
in your home directory. This log not only serves as a record but also powers the immediate recall functions of bash.
You can view your history by simply typing history
. Each command is typically prefixed with a number, indicating its position in the list. This form of notation is not just for reading; it's actionable, as we'll explore.
1. Reusing Commands with !
At its simplest, the exclamation mark !
can be a powerful tool:
Repeat the last command: Simply typing
!!
repeats the last command executed.Reuse a specific command: If you remember the number of a command from using
history
,!number
will execute that specific command again.Command by search: Executing
!ssh
would run the most recent command that starts with "ssh," making it extremely useful for revisiting lengthy or complex commands.
2. Navigate with the Arrow Keys
Perhaps the most intuitive method: the Up Arrow ↑
and Down Arrow ↓
cycle through recent commands. It's perfect for rapid, linear browsing of your history.
3. Search and Execute with Ctrl + r
For a more robust way to find commands, use the reverse search function by hitting Ctrl + r
and then typing a keyword. This searches your history as you type. Once you see the command you want, press Enter to execute it or Ctrl + j
to select the command without running it.
4. Edit Commands with fc
If you need to edit a command from history before executing it, fc
is your tool. Typing fc
opens the most recent command in your default editor. You can also specify a range, like fc 10 20
, to edit all commands from the 10th to the 20th.
5. Customise Your History
You can modify the behavior of your bash history for better efficiency:
Increase history size: By default, bash may only remember the last 500 commands. You can increase this by setting
HISTSIZE
andHISTFILESIZE
in your.bashrc
file.Ignore duplicate entries:
HISTCONTROL=ignoredups
orHISTCONTROL=ignoreboth
can be set to avoid logging redundant commands.Exclude minor commands: By setting
HISTIGNORE
, you can specify patterns to exclude from history, such asls
,bg
,fg
,exit
.
6. Manipulate History Output
Modify how your history is displayed to make it even more useful:
Timestamp your history: Adding
HISTTIMEFORMAT="%d/%m/%y %T "
in your.bashrc
will append the time and date to each command in your history, aiding in recall of when exactly a command was executed.Clean up history: Commands like
history -c
(clear all) andhistory -d offset
(delete a specific entry) help in maintaining a tidy and relevant list.
Conclusion
Mastering bash's history commands can significantly speed up your command line operations by reducing redundancies and preventing the need to repeatedly type or look up complex commands. As with any tool, the more you practice with these history hacks, the more intuitive they will become, streamlining your interactions with the bash shell. Try incorporating these into your daily workflow and watch your productivity soar!