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


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
$ ls -l `!!`
ls -l `which php`
lrwxrwxrwx 1 root root 21 2008-06-12 02:47 /usr/bin/php -> /etc/alternatives/php
$ _

Leave a Reply