I learned a fun bash trick about a week 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:
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 has an RHCSA for Red Hat Enterpirse Linux 7 and currently works as a Linux Systems Adminstrator in Ohio.
This post, re-published here with permission, wasÂ originally published on Luke’s site here.
Damn, this is gonna save me a lot of tedium