Ted Leung on the air
Ted Leung on the air: Open Source, Java, Python, and ...
Mon, 20 Jun 2005
From Bash to Z-Shell

I have a long history with UNIX. My first exposure was to 2BSD on the VAX. The shell on that system was the C-Shell. When I got to MIT and started working at Athena, I was using tcsh. During my years at Brown, I discovered zsh, which I am using to this very day.

Most of the time I picked up what I needed to know by reading the man pages for the various shells, and it's been a very long time since I read a book on a shell. I think that the only shell books that I own are The UNIX Programming Environment and The KornShell command and programming language. Recently, I've wanted to do more stuff with zsh, particularly around command completions. zsh is on it's second completion system, and I found the manual pages pretty tough going. So I finally broke down and got a copy of "From Bash to Z Shell: Conquering the Command Line". Two of the authors are zsh developers, which made me very hopeful about the book.

I was not disappointed. Maybe it's just that I needed a shell refresher in general, but I got a lot out of the book. Here are a few of the things that jumped out from my notes:

  • redirecting the output of a for loop
  • the CDPATH variable (cd searches this path)
  • accept-line-and-down-history [Ctrl-o] (make it easy to execute sequence of lines from the history)
  • push-line-or-edit and the zsh buffer stack (push fragments of a command line onto a stack)
  • The !? history command (search history for nearest match)
  • The two argument form of the cd command (replace arg1 in $cwd w/ arg2 and cd there)
  • Matching numeric ranges (understands numbers)
  • bashcompinit (emulate bash completions commands - allows use of bash_completion project)
  • Partial completion (cd /u/lo/b <tab> to cd to /usr/local/bin)
  • Process substitution ( emacs =(indent -st < file) - sends output of indent to emacs)
  • zsh associative arrays (very useful)

The chapters on zsh completion alone were worth the price for me.

For those new to zsh, Yammer has some other zsh examples.

[10:43] | [computers/programming] | # | TB | F | G | 4 Comments | Other blogs commenting on this post
I'm not seeing a lot new there.  zsh seems okay, but I tried making use of it and rapidly got annoyed with the "same stuff, different syntax" compared to bash or ksh.

  * redirecting the output of a for loop

(for i in *; do echo $i; done) |less

  * the CDPATH variable (cd searches this path)

Easy to do with a function in bash.

  * accept-line-and-down-history [Ctrl-o] (make it easy to execute sequence of lines from the history)

Kinda useful, I guess.  But if you're doing a dependent sequence of commands, you should put them in a single command line with &&

  * push-line-or-edit and the zsh buffer stack (push fragments of a command line onto a stack)

#partial command

  * The !? history command (search history for nearest match)

man bash, read the HISTORY section.

  * The two argument form of the cd command (replace arg1 in $cwd w/ arg2 and cd there)

Easy to write as a function.

  * Matching numeric ranges (understands numbers)
  * bashcompinit (emulate bash completions commands - allows use of bash_completion project)
  * Partial completion (cd /u/lo/b <tab> to cd to /usr/local/bin)

cd /u<tab>/lo<tab>/b<tab>, and it's a lot more likely to get you where you wanted.

  * Process substitution ( emacs =(indent -st < file) - sends output of indent to emacs)

indent -st <file |emacs, or indent >out && emacs out

  * zsh associative arrays (very useful)

Sure.  Though you can use numeric arrays in bash, and ksh has associative arrays.  In no case should you be doing complex programming in a shell script!



A good resource for sh, bash, and ksh is
Stephen Kochan's Unix Shell Programming.
Posted by Mark Hughes at Mon Jun 20 13:05:09 2005




You can subscribe to an RSS feed of the comments for this blog: RSS Feed for comments

Add a comment here:

You can use some HTML tags in the comment text:
To insert a URI, just type it -- no need to write an anchor tag.
Allowable html tags are: <a href>, <em>, <i>, <b>, <blockquote>, <br/>, <p>, <code>, <pre>, <cite>, <sub> and <sup>.

You can also use some Wiki style:
URI => [uri title]
<em> => _emphasized text_
<b> => *bold text*
Ordered list => consecutive lines starting spaces and an asterisk

Name:


E-mail:


URL:


Comment:


Remember my info?


twl JPG

About

Ted Leung FOAF Explorer

I work at the Open Source Applications Foundation (OSAF).
The opinions expressed here are entirely my own, not those of my employer.

Creative Commons License
This work is licensed under a Creative Commons License.

Now available!
Professional XML Development with Apache Tools : Xerces, Xalan, FOP, Cocoon, Axis, Xindice
Technorati Profile
PGP Key Fingerprint
My del.icio.us Bookmarks
My Flickr Photos


Syndicate
RSS 2.0 xml GIF
Comments (RSS 2.0) xml GIF
Atom 0.3 feed
Feedburner'ed RSS feed

< June 2005 >
SuMoTuWeThFrSa
    1 2 3 4
5 6 7 8 91011
12131415161718
19202122232425
2627282930  

Archives
2006
2005
2004
2003

Articles
Macintosh Tips and Tricks

Search
Lucene
Blogs nearby
geourl PNG

Categories
/ (1567)
  books/ (33)
  computers/ (62)
    hardware/ (15)
    internet/ (58)
      mail/ (11)
      microcontent/ (58)
      weblogs/ (174)
        pyblosxom/ (36)
      www/ (25)
    open_source/ (145)
      asf/ (53)
      osaf/ (32)
        chandler/ (35)
        cosmo/ (1)
    operating_systems/ (16)
      linux/ (9)
        debian/ (15)
        ubuntu/ (2)
      macosx/ (101)
        tips/ (25)
      windows_xp/ (4)
    programming/ (156)
      clr/ (1)
      dotnet/ (13)
      java/ (71)
        eclipse/ (22)
      lisp/ (34)
      python/ (86)
      smalltalk/ (4)
      xml/ (18)
    research/ (1)
    security/ (4)
    wireless/ (1)
  culture/ (10)
    film/ (8)
    music/ (6)
  education/ (13)
  family/ (17)
  gadgets/ (24)
  misc/ (47)
  people/ (18)
  photography/ (25)
    pictures/ (12)
  places/ (3)
    us/ (0)
      wa/ (2)
        bainbridge_island/ (17)
        seattle/ (13)
  skating/ (6)
  society/ (20)



[Valid RSS]

del.icio.us linkblog

www.flickr.com

Blogroll

java.blogs
Listed on BlogShares

Locations of visitors to this page
Where are visitors to this page?


pyblosxom GIF