bash Tips & Tricks

By | 1 Aug 2008

Bourne Again Shell.

bash magic

Some handy bash magic.

sudo last command

sudo !!

If you forgot sudo after executing your 3-lines-command, sudo bang! bang! will repeat the last entered command with sudo prefixed.

More parameter magic

bangExpands to
!$last argument of previous command
!$:pjust show last argument of previous command, don’t add to commandline
!*all arguments of previous command
!!:1first argument of previous command
!vilast command that started with “vi“
!vi:pjust show last “vi“-call, don’t run it again
^err^corrreplace all occurrences of err by corr in the last command

Shortcuts

keypressDescription
Ctrl+wErase word
Ctrl+uErase from cursor to beginning of line
Ctrl+aMove cursor to beginning of line
Ctrl+eMove cursor to end of line
Ctrl+rSearch command history (type letters after this)

chdir to last one

cd -

Changes to previous directory.

Use output of previous command

Sometimes it’s handy to use the output of a previous command, e.g. a which. To do that, simply use the bang-bang with the backtick operator:

$ which php
/usr/bin/php
$ ls -l `!!`
ls -l `which php`
lrwxrwxrwx 1 root root 21 2008-06-12 02:47 /usr/bin/php -> /etc/alternatives/php
$ _Code language: JavaScript (javascript)

Leave a Reply

Your email address will not be published. Required fields are marked *

Your post must include a link to this post.

How does this work?
Write a response on your own website and include a link to this post. Then submit your response’s URL here. Your response may appear here after moderation. To update or remove it, edit or delete your original post and submit the same URL again. Learn more about Webmentions.