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


Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.


Rohit's picture

Even $_ works in same way. However not exactly in same way to the one mentioned in this post but somehow it works.


# ls /home/usr/

Now if you want to relist the content of /home/usr directory then you the following:

# ls $_

Always more to learn

mattcen's picture

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) :-).

Matthew Cengia


wom's picture

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 !!

Anonymous's picture

Didn't Shawn Powers just cover this in his tech tips?

Thanks for the tips. Esc-.

Anonymous's picture

Thanks for the tips.

Esc-. will also paste the last command-line argument.

White Paper
Linux Management with Red Hat Satellite: Measuring Business Impact and ROI

Linux has become a key foundation for supporting today's rapidly growing IT environments. Linux is being used to deploy business applications and databases, trading on its reputation as a low-cost operating environment. For many IT organizations, Linux is a mainstay for deploying Web servers and has evolved from handling basic file, print, and utility workloads to running mission-critical applications and databases, physically, virtually, and in the cloud. As Linux grows in importance in terms of value to the business, managing Linux environments to high standards of service quality — availability, security, and performance — becomes an essential requirement for business success.

Learn More

Sponsored by Red Hat

White Paper
Private PaaS for the Agile Enterprise

If you already use virtualized infrastructure, you are well on your way to leveraging the power of the cloud. Virtualization offers the promise of limitless resources, but how do you manage that scalability when your DevOps team doesn’t scale? In today’s hypercompetitive markets, fast results can make a difference between leading the pack vs. obsolescence. Organizations need more benefits from cloud computing than just raw resources. They need agility, flexibility, convenience, ROI, and control.

Stackato private Platform-as-a-Service technology from ActiveState extends your private cloud infrastructure by creating a private PaaS to provide on-demand availability, flexibility, control, and ultimately, faster time-to-market for your enterprise.

Learn More

Sponsored by ActiveState