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".
Trending Topics
| You Need A Budget | Feb 10, 2012 |
| The Linux powered LAN Gaming House | Feb 08, 2012 |
| Creating a vDSO: the Colonel's Other Chicken | Feb 06, 2012 |
| Your CMS Is Not Your Web Site | Feb 01, 2012 |
| Casper, the Friendly (and Persistent) Ghost | Jan 31, 2012 |
| Razor-qt 0.4 - Qt based Desktop Environment | Jan 30, 2012 |
- Link to modlys
51 min 38 sec ago - I use YNAB because of the
1 hour 3 min ago - Search
6 hours 6 min ago - Question
6 hours 29 min ago - for the record
6 hours 31 min ago - That's disappointing. Thanks
8 hours 55 min ago - Well spotted. I've corrected
10 hours 24 min ago - This is a great program. We
13 hours 24 min ago - No Air for Linux
15 hours 13 min ago - HEWLETT PACKARD created
15 hours 23 min ago





Comments
$_
Even $_ works in same way. However not exactly in same way to the one mentioned in this post but somehow it works.
e.g.
# ls /home/usr/
Now if you want to relist the content of /home/usr directory then you the following:
# ls $_
Always more to learn
Thanks for the information on 'Esc-.'; I hadn't heard about that one, and can't find documentation about it in the Bash manpage!
Shawn did indeed cover (part of) this tech tip in one of his recent videos, so I suppose you could say this serves as a re-cap and elaboration on it (some of which was also mentioned in the comments to Shawn's video) :-).
--
Regards,
Matthew Cengia
alt-.
I use alt-. to cycle last section of previous command. Also; "fc" if I want to edit the entire command and run it again in default editor.
sudo !!
Didn't Shawn Powers just cover this in his tech tips?
Thanks for the tips. Esc-.
Thanks for the tips.
Esc-. will also paste the last command-line argument.