DEV Community

Cover image for Bash Shell Tricks

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 m...
Collapse
 
val_baca profile image
Valentin Baca • Edited

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 !$
Enter fullscreen mode Exit fullscreen mode

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

Collapse
 
slariviere profile image
Sébastien Larivière

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

Collapse
 
val_baca profile image
Valentin Baca

Even better! Thanks

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

Collapse
 
bhaskar_vk profile image
Bhaskar Karambelkar

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

Collapse
 
geoff profile image
Geoff Davis • Edited

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

...you have to execute...

!!:gs/command/echo

...to output:

first echo
second echo!
Enter fullscreen mode Exit fullscreen mode

Further reading: Stack exchange

Collapse
 
bhaskar_vk profile image
Bhaskar Karambelkar

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

Collapse
 
kav2k profile image
Alexander Kashev • Edited

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

Collapse
 
bhaskar_vk profile image
Bhaskar Karambelkar

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.

Collapse
 
kav2k profile image
Alexander Kashev • Edited

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.

Collapse
 
jorinvo profile image
jorin

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: twitter.com/qvlio

Collapse
 
eduardort profile image
Eduardo Reyes

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

Collapse
 
bobbyiliev profile image
Bobby Iliev

This is a great article!

If you are a fan of opensource, feel free to contribute to the Introduction to Bash Scripting open-source eBook on GitHub!

Collapse
 
pratikaambani profile image
Pratik Ambani

+1 for the first one. 👌👌

Collapse
 
oyagci profile image
Oguzhan Yagci • Edited

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