The caret is mightier than the up arrow

By: Luke Rawlins Mar 13, 2018 | 2 minutes read
Share this:

Tags: Bash, command line, Linux

I learned a fun bash trick a while ago that I thought I would share. In a bash shell you can use the caret ^ symbol to find and replace a sequence of characters in your previous command.

For instance if you type:

sudo systemctl restart httpd

and then want to look at the status of the httpd service all you need to do is:

^restart^status

Bash will look at the last command in your history and replace the first occurrence of “restart” with “status” and run the new command.

Over the last week or so I’ve found that I get the most use out of this trick from my atrocious spelling. More often than not I spell “systemctl” as “systemclt”, or instead of “sudo something” I type “sodu something.” Using the caret syntax I can quickly fix my spelling mistakes in the command line without having to retype long strings  that had a couple of letters out of place.

The other thing this is useful for, is to show off your awesome command line skills and see the looks of adoration you get from your fellow Linuxy people. In fact, to be honest, that is probably the best reason to learn these kinds of things. :)

So next time you are about to press the up arrow and fix a spelling mistake, or change a command option try using the ^oldstring^newstring trick instead.

Till next time

— Luke

Related Posts


Working with logical Volumes (part 3)

Following this tutorial assumes that you have followed along with the other two parts in this series. However, if you already have some familiarity with Linux you should be able to follow along. Working with logical volumes (part 1) Working with logical volumes (part 2) Add a disk to the volume group One of the great things about lvm is that you can add and remove physical volumes on the fly without data loss and without interrupting services. Read more

Working with logical volumes (part 2)

In this post I want to cover one of the most commonly used features of lvm, extending a logical volume. If you were following along with the last post, “Working with logical volumes part 1”, then you should already have a volume group with a couple of live volumes attached. With lvm you can quickly and easily extend a Linux file system on the fly without interrupting any services. Becoming familiar with our lvm environment. Read more

Open multiple files with vim

There are many instances when it’s useful to have multiple files open in vim, but if you aren’t familiar with this tool you can find yourself needlessly jumping around between multiple windows. If you are doing any type of real systems work on a Linux operating system I suggest that you familiarize yourself with vim. If you are not already using vim start by opening a command prompt and type vimtutor, once you’ve become familiar with how to navigate, search, and edit a document with vim this post will make more sense to you. Read more


Contact

If you’d like to get in touch, contact with me via email - or follow on Twitter.

editor@sudoedit.com