Tech Tip: Use History Expansion To Save Time and Typing
Most shells maintain a history of the commands that you've typed and that history can be used to save time and typing. Most of us have probably used the up/down arrow keys already to move through the list of commands that we've already entered, but there are other ways to access the data in the history list.
Let's say you want to install a package (we'll use a Debian/Ubuntu-based system as an example), so you type:
$ apt-get install packagename
But wait a minute, you need 'sudo' to do that! So what do you do? Retype the entire command again? No, use history expansion and type:
$ sudo !!
This will run the entire last command with 'sudo' prepended to it!
Let's take another example, say you want to list a directory's contents before changing into it, so you use ls to see the directory:
$ ls /usr/share/doc
Now, rather than typing the entire path name again as an argument to cd command, you simply type:
$ cd !$
This takes the last argument from the the last command entered and inserts it in the new command (use !* to include all the arguments from the last command).
Most shells provide many more expansion features that can save time and typing, check your shell's documentation for more information. For bash search for "History Expansion" or "Event Designators".
-- Regards, Matthew Cengia
|SUSE Linux Enterprise High Availability Extension||Mar 29, 2017|
|Hybrid Cloud Storage Delivers Performance and Value||Mar 29, 2017|
|smbclient Security for Windows Printing and File Transfer||Mar 28, 2017|
|How to Calculate Flash Storage TCO||Mar 27, 2017|
|Non-Linux FOSS: Don't Drink the Apple Kool-Aid; Brew Your Own!||Mar 27, 2017|
|Three EU Industries That Need HPC Now||Mar 25, 2017|
- smbclient Security for Windows Printing and File Transfer
- Hybrid Cloud Storage Delivers Performance and Value
- SUSE Linux Enterprise High Availability Extension
- Returning Values from Bash Functions
- Preseeding Full Disk Encryption
- William Rothwell and Nick Garner's Certified Ethical Hacker Complete Video Course (Pearson IT Certification)
- Non-Linux FOSS: Don't Drink the Apple Kool-Aid; Brew Your Own!
- Readers' Choice Awards 2011
- Hodge Podge
- HOSTING Monitoring Insights