Bash Shell Tricks

Bhaskar Karambelkar on July 09, 2017

This was originally published on my blog back in 2007, I am glad to see that this has aged well. I still find personally using many of the tricks... [Read Full]
markdown guide

Neat tips! thanks

Here's mine:

We all know !! but it's mostly just used to sudo !!
I find !$ (which is last argument of the last command) incredibly useful:

#make a directory and go to it
mkdir workspace
cd workspace

# OR even better
mkdir workspace
cd !$

# move then edit a file
mv ~/somefile.txt ~/renamed.txt
vim !$

# edited a file, now add it to git
vim awesome_code.c
git add !$

There's a whole slew of ! commands but !$ is one I use all the time.


You can also use esc + . instead of !$. I find more natural and useful if you need to fix a typo.


Even better! Thanks

P.S. For posterity, all of these work just as well in zsh.


Funny $_ is the exact same as !$. There's always more to learn 😀.


Great write-up!

Quick note, if you are using the command edit/replace feature and are using a conditional command construct (echo "first command" && echo "second command!") the ^foo^bar syntax only replaces the first instance of the searched text.

To replace every instance of that searched text, you have to use !!:gs/foo/bar; going back to my first example:

To replace all the instances of command in...

echo "first command" && echo "second command!" have to execute...

!!:gs/command/echo output:

first echo
second echo!

Further reading: Stack exchange


Yup I found out about it sometime after I wrote the original post almost 10 years ago. Thanks for the reminder!


Small correction: bash completion for SSH looks for $HOME/.ssh/config, not authorized_keys


Yikes my bad. Actually it should be $HOME/.ssh/known_hosts file for auto completing host names. You won't find host names in $HOME/.ssh/config unless you've explicitly put them there, but every host you connect to gets saved in known_hosts by default.


Yes, it does, but bash-completion does not read hosts from known_hosts - at least on my Ubuntu 16.04 machine.

It will, however, read both hostnames and host aliases from config.


Nice writeup! I definitely have look more into /dev/*!

I learn something new that unix tools can do basically every day 😄 In case someone is interested, I try to share those findings on a separate Twitter account:


So cool! I also use $_ for the arguments of the last command, echo "something" $_ will be "something"


I love shells. They are so powerful. If I can't use one on any of my devices I feel naked

code of conduct - report abuse