Small Mosaic


Categories:

/books
/career
/codinghorrors
/events
/geekstuff
/justdont
/languages
/languages/bash
/linkshot
/magazines
/meta
/misctech
/movies
/nottech
/operatingsystems
/operatingsystems/linux
/operatingsystems/linux/debian
/operatingsystems/solaris
/perl
/presentations
/programming
/python
/ruby
/security
/security/apache
/security/tools
/serversmells
/services
/services/dns
/sites
/specifications
/sysadmin
/testing
/tools
/tools/commandline
/tools/firefox
/tools/gui
/tools/network
/tools/online
/tools/online/greasemonkey
/tools/puppet
/unixdaemon

Archives:

July 20111
June 20112
May 20113
April 20112
March 20117
January 20111
December 20103
November 20103
August 20101
July 20101
June 20104
May 20102
April 20101
March 20108
February 20101
January 20102
Full Archives

Wed, 01 Feb 2006

Preserving Command Line Loop Formatting in Bash
If you're a heavy bash user you'll often find yourself writing short snippets of code on the command line. Typically they'll be based around a main loop and you'll end up entering them over multiple lines to keep them readable. Unfortunately when you try reuse the command, by retrieving it from the bash command history, it'll be transformed in to one semicolon laden unreadable mass. Unless you read on...

One of the options bash allows you to set is 'lithist'. If you enable it, either for the current session, by entering 'shopt -s lithist' or by putting adding the command to your .bash_profile or .bashrc all future multi-line commands will be correctly preserved. You'll go from this:

$ for file in `ls *`; do command $file; otherthing $file; done

To this:

$ for file in `ls *`
do
  command $file
  otherthing $file
done

Which as you can see is much easier to read. One note though, enabling this option means that commands are also stored with embedded newlines in the .bash_history file. While this isn't necessarily a problem it does mean that grepping through that file may only return parts of a single command.

Like this post? - Digg Me! | Add to del.icio.us! | reddit this!

Posted: 2006/02/01 23:58 | /tools/commandline | Permanent link to this entry | This entry and same date


books career codinghorrors events geekstuff justdont magazines meta misctech movies nottech operatingsystems/linux operatingsystems/linux/debian operatingsystems/solaris perl programming python ruby security security/apache security/tools serversmells services/dns sites sysadmin testing tools tools/commandline tools/firefox tools/gui tools/network tools/online tools/online/greasemonkey tools/puppet unixdaemon

Copyright © 2000-2010 Dean Wilson XML feed logo