Fork me on GitHub

Best Bash tip ever! 7

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

Comments

Leave a response

  1. AlbertDecember 31, 2008 @ 01:33 PM
    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.

  2. OmariDecember 31, 2008 @ 02:14 PM

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

    Bash-Z-Shell-Conquering-Command

  3. QuimbyDecember 31, 2008 @ 02:31 PM
    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. greatDecember 31, 2008 @ 03:41 PM

    OLD TIP IS OLD!!

  5. mirabilosDecember 31, 2008 @ 04:05 PM

    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.mirbsd.org:/usr/src/distrib/generic/obj/build/GENERIC
    tg@bleu:~ $ r 2=3
    head -3 /var/run/dmesg.boot
    MirBSD#10uA4 (GENERIC) #1161: Fri Dec 26 21:05:59 UTC 2008
            tg@bleu.mirbsd.org:/usr/src/distrib/generic/obj/build/GENERIC
    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.mirbsd.org:/usr/src/distrib/generic/obj/build/GENERIC
    tg@bleu:~ $ print $_ 
    /var/run/dmesg.boot
    Instead of “^-s” you would use “r -- -s=” (the two dashes are needed as the “r” built-in alias parses its arguments).
  6. SamDecember 31, 2008 @ 05:39 PM

    http://samrowe.com/wordpress/advancing-in-the-bash-shell/ has that and more.

  7. eolo999January 01, 2009 @ 11:13 PM

    See this: http://successtheory.com/tips/zshtips.html