DEV Community

Wynand Pieters
Wynand Pieters

Posted on

My favourite Bash feature

Just a quick one here, but I used this a lot today and realised after chatting to some other devs that a lot of people don't know about this trick.

That trick is: History Expansion

Let's say you are SSH'd (or SSM'd on AWS if you want to be more secure) onto a Linux box and you want check the status of a service with

systemctl status cassandra.service
Enter fullscreen mode Exit fullscreen mode

You see that the service is exited for some reason, and you want to restart it after determining that's all that is needed.

Do you now go and type out

systemctl restart cassandra.service
Enter fullscreen mode Exit fullscreen mode

or do you want to be 1337 and impress all your friends with

^status^restart
Enter fullscreen mode Exit fullscreen mode

We all know you prefer the latter.

There are some variations on this, I recommend you check out the docs (or man pages for the really 1337 among you).

What are some of your favourite Bash tricks?

Top comments (1)

Collapse
 
arutonee profile image
arutonee

I love the Emacs keybindings, which are a bit more flexible than !! or ^.
<C-p><M-b><M-b><C-w>restart may be less 1337, but it uses 3 less keystrokes...

the ones that I use often are:

  • <C-p><C-n><C-f><C-b> for up, down, left, and right respectively
  • replace C (ctrl) with M (usually alt) for navigation by word instead of by character
  • <C-w> to delete a word (Vim also has this)
  • <C-a> to go to the start of the line
  • <C-e> to go to the end of the line

among many more...