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.
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:
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.
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
I am a professional DevOps Engineer with a demonstrated history of working in the internet industry. I am an avid Linux lover and supporter of the open-source movement philosophy.
Neat tips! thanks
Here's mine:
We all know
!!but it's mostly just used tosudo !!I find
!$(which is last argument of the last command) incredibly useful: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^barsyntax 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
commandin...echo "first command" && echo "second command!"...you have to execute...
!!:gs/command/echo...to output:
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, notauthorized_keysYikes my bad. Actually it should be
$HOME/.ssh/known_hostsfile for auto completing host names. You won't find host names in$HOME/.ssh/configunless you've explicitly put them there, but every host you connect to gets saved inknown_hostsby 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: twitter.com/qvlio
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!
So cool! I also use
$_for the arguments of the last command,echo "something"$_will be "something"+1 for the first one. 👌👌
I love shells. They are so powerful. If I can't use one on any of my devices I feel naked