Best Bash tip ever!

Posted by – 31/12/2008

While googling for something completely different (which I cannot remember right now), I passed by what is probably the best Bash tip I ever read:

bash$ head -10 /etc/apache2/sites-available/default
bash$ ^10^20^

What happens in the second command is that the previous one is repeated, replacing “10” by “20”! How great is that?!

7 Comments on Best Bash tip ever!

  1. mirabilos says:

    Some comments I put at our Developers’ Weblog

    Of course, mksh(1) can do it as well:

    tg@bleu:~ $ head -2 /var/run/dmesg.boot
    MirBSD#10uA4 (GENERIC) #1161: Fri Dec 26 21:05:59 UTC 2008
    tg@bleu:~ $ r 2=3
    head -3 /var/run/dmesg.boot
    MirBSD#10uA4 (GENERIC) #1161: Fri Dec 26 21:05:59 UTC 2008
    cpu0: Intel(R) Pentium(R) M processor 1.40GHz ("GenuineIntel" 686-class) 598 MHz
    tg@bleu:~ $ fc -l
    1       head -2 /var/run/dmesg.boot
    2       head -3 /var/run/dmesg.boot

    What’s best, the modified commands are written into the history, not the modificator itself.

    Some of the commentaries are rather clueless too, not $! but $_ is the last word of the last command, in this case:

    tg@bleu:~ $ head -2 /var/run/dmesg.boot
    MirBSD#10uA4 (GENERIC) #1161: Fri Dec 26 21:05:59 UTC 2008
    tg@bleu:~ $ print $_

    Instead of “^-s” you would use “r — -s=” (the two dashes are needed as the “r” built-in alias parses its arguments).

  2. great says:


  3. Quimby says:

    yeah i find that ^old^new really useful.

    another tip:

    bash$ aptitude -s install zsh
    bash$ ^-s

    the last line will remove the -s option and thus install zsh

  4. Omari says:

    Check out this book, it’s got all the tips you could want:


  5. Albert says:

    You probably know this one, but I’ll share for others who might not:

    bash$ ls /var/www/apache2-default/
    bash$ touch $!

    The $! reuses the last argument of the previous command.

Leave a Reply

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