Slashdot Mirror


From Bash To Z Shell

r3lody (Raymond Lodato) writes "Novice users and power users of *nix will enjoy reading From Bash to Z Shell: Conquering the Command line by Oliver Kiddle, Jerry Peek, and Peter Stephenson. In this moderate-sized book from Apress, the authors delve into both bash (the Bourne Again Shell) and zsh (Z Shell) to enable you to use them to their fullest advantage. Topics range from the simple editing of the command line to redefining key sequences, down into creating functions for editing and command-line completion. Some areas are covered in other books, but this one goes into some little-seen side streets and alleyways to show you the shortcuts to more efficient use of the shell." Read on for the rest of Lodato's review. From Bash to Z Shell: Conquering the Command Line author Oliver Kiddle, Jerry Peek, and Peter Stephenson pages 472 publisher Apress rating 9 reviewer Raymond Lodato (rlodato AT yahoo DOT com) ISBN 1590593766 summary An in-depth look at the functionality of bash and zsh.

A *nix-style shell is available on a number of platforms, so the authors chose not to limit themselves to just one, such as Linux. The techniques they discuss can be used in Unix, as well as under Windows using cygwin.

In case you're not overly well-versed in shell handling, the first part of the book does a pretty good job covering all of the things a typical user might want to do. Basic command editing, I/O redirection, jobs, processes, and some simple scripting are all covered. For many users, this is also as far as they would like to go. However, reading a little further yields treasure.

The next part delves into bash version 3.0 and zsh version 4.2, both freely available on the Internet. In addition to more sophisticated command line editing techniques, the authors also delve into the misty realms of re-binding keys. A great many users find themselves typing the same sequences over and over again. While sometimes a script makes sense to encapsulate these sequences, sometimes you want to simply enter some text and that's where a key binding makes sense. One example given in the book for zsh is bindkey -s '\C-xt' 'March 2004\eb' . After the binding, typing CTRL-x t puts the string 'March 2004' onto the command line, and moves the cursor under the '2' so you can insert the day of the month. That's a very simple example for a very powerful facility. A good chunk of chapter 4 is spent on showing how to make the most of bindkey (or its bash cousin 'bind').

The next few chapters cover common topics of prompt strings, file/directory globbing, and shell history. Then, significant press is given to the subject of pattern matching. Many people understand basic pattern matching and regular expressions, but From Bash to Z Shell goes into careful detail, with many examples from both bash and zsh, to contrast the (minor) differences between these two powerful shells. The next chapter discusses command line and file/directory name completion, a topic usually glossed over in other texts. Finally, job processing wraps up Part 2.

The third and final part of the book deals with extending the shell using variables, scripts, and functions. Here's where we get into the nitty-gritty. The first two chapters go over familiar territory: shell variables and shell programming. The chapter on programming is easy to follow, and I suggest you try the examples as you go to get the most out of it. The last two chapters focus on topics frequently overlooked: editor functions, and completion functions. Editor functions allow you (with bind[key]) to define new capabilities to use while editing the command line. This is where a true power user can shine, creating a suite of new functions to speed his/her use of zsh or bash. Completion functions work in defining new ways for the shell to complete a command, file name, or directory, based on a user-written function. Honestly, it's not something I would tend to use, but the capability is intriguing.

All in all, From Bash to Z Shell provides a frequent shell user with a plethora of new insights into customizing the bash and zsh shell programs to fit his/her tastes. The authors have filled a void in tackling the subject of customizing the shell rather than just simply using it. I would have liked to see more coverage of some of the more standard uses of the shells, just so the book could be a more complete reference, rather than the specialized one it is. Specialized or not, there is a lot offered here, and you couldn't go wrong getting this book.

You can purchase From Bash to Z Shell: Conquering the Command Line from bn.com. Slashdot welcomes readers' book reviews -- to see your own review here, read the book review guidelines, then visit the submission page.

55 of 214 comments (clear)

  1. Which one? by Anonymous Coward · · Score: 4, Funny

    Which shell has a better wallpaper? More options when I right-click?

    1. Re:Which one? by SIGALRM · · Score: 3, Funny
      #!/bin/bash

      source tfa
      [ "$?" -eq 0 ] && return 1
      --
      Sigs cause cancer.
    2. Re:Which one? by Anonymous Coward · · Score: 4, Funny

      My Bash console shell has a screenshot from Doom III as the wallpaper. At least it looks that way...

  2. Wouldn't it have been better... by jgerry · · Score: 4, Insightful

    Uh, wouldn't it have been cooler to actually go the extra mile with the whole A to Z metaphor and call the book "From Ash to Z Shell"? There is a shell called ash.

    1. Re:Wouldn't it have been better... by AKAImBatman · · Score: 3, Funny

      "From Ash to Z Shell"?

      Technically, that should be either "From ash to zsh" or "From A to Z Shell".

    2. Re:Wouldn't it have been better... by exley · · Score: 4, Insightful

      Well, considering that the focus appears to be just on bash and zsh, I'd say no. With that in mind, however, perhaps they could have spent more time choosing a more concise title.

    3. Re:Wouldn't it have been better... by Len · · Score: 2, Informative
      or "From A to Z Shell".

      Technically that's not right either because ash is the Adventure Shell!

    4. Re:Wouldn't it have been better... by AKAImBatman · · Score: 2, Informative

      Ummm... no. That would be a different thing. Ash is the Almquist Shell, an open source replacement for /bin/sh (the bourne shell).

    5. Re:Wouldn't it have been better... by jondt · · Score: 3, Informative

      except, if you'd read the article, you would have noticed that the author only covers bash and zsh. Ash is given but a cursory glance.

    6. Re:Wouldn't it have been better... by Brandybuck · · Score: 3, Interesting

      Which is a shame because the world ends up with more bash-only and zsh-only scripts. As a user give me all the bash goodness you can, but as a scripter give me the lingua franca that is the vanilla bourne shell.

      I say this because I once spent months fixing hundreds of scripts in an embedded system after the OS vendor upgraded from bash-1.x to bash-2.x. If you don't need the extras that bash gives you, then use the #!/bin/sh shebang and make sure it works with sh/ash/ksh.

      --
      Don't blame me, I didn't vote for either of them!
    7. Re:Wouldn't it have been better... by AKAImBatman · · Score: 2, Informative

      when I first encountered it nearly two decades ago, it was installed as "ash".

      If you're referring to the original script version as opposed to the C port, then the shell name was "ash.sh". Someone might have copied or symlinked it to just "ash" on your system. Here's the original Adventure Shell from Usenet. Note the 1987 timestamp.

  3. Oooo, religious wars!! by smcdow · · Score: 5, Funny

    It's like the good old days!!!

    zshell sux!! bash rules!!

    --
    In the course of every project, it will become necessary to shoot the scientists and begin production.
    1. Re:Oooo, religious wars!! by AKAImBatman · · Score: 2, Interesting

      Did anyone ever actually argue over ZShell? I thought the primary arguments were always the BASH vs. KSH arguments (i.e. general users vs. hardcore unix admins) and the TCSH vs. BASH arguments (there is *some* validity to TCSH being a bit more professional, but BASH is just more useful).

    2. Re:Oooo, religious wars!! by smittyoneeach · · Score: 2, Funny

      Likely the former, or he'd have touted eshell.el.

      --
      Get thee glass eyes, and, like a scurvy politician, seem to see things thou dost not.--King Lear
    3. Re:Oooo, religious wars!! by LurkerXXX · · Score: 2, Funny

      What are you talking about? Everyone knows pico is the only editor worth using!

    4. Re:Oooo, religious wars!! by AKAImBatman · · Score: 3, Informative

      Short answer: Yes

      Long answer: I don't think most tcsh users use it for its programming ability. Instead, it tends to be useful as a solid interactive shell. Its general "feel" tends to be more solid than bash, owing partly to the fact that bash has a lot of built in key-shortcuts that tcsh doesn't. Of course, once you're used to being able to double tab for a directory listing, it's kind of hard to give up.

    5. Re:Oooo, religious wars!! by Anonymous Coward · · Score: 5, Funny

      How, exactly, is tcsh more professional

      I once caught bash selling my personal files on ebay. Also, tcsh cured my grandmother of rheumatoid arthritis.

      In related news, emacs is more articulate than vi, three-button mice are sweeter than one-button mice and perl grepped your sister.

      Actually, that last one is true ;-> Sorry.

    6. Re:Oooo, religious wars!! by sp0rk173 · · Score: 2, Informative

      that kind of tab completion exists in tcsh too - just toss set autolist in your .cshrc file. I had it enabled on my last freebsd install - basically makes tcsh work exactly like bash while still feeling more solid. Silly linux kids, when will they learn?!

    7. Re:Oooo, religious wars!! by pilgrim23 · · Score: 2, Interesting

      Well actually, the True Believer will go waaaaay back before this Unix upstart and enter the command CATALOG from a Apple Basic 3.3 prompt, or if "modern school", CAT on a ProDOS line.

      Everytime I try to understand a *nix shell parm I have to switch to Emacs and ask the Doctor....

      --
      - Minutus cantorum, minutus balorum, minutus carborata descendum pantorum.
    8. Re:Oooo, religious wars!! by AKAImBatman · · Score: 3, Informative

      IIRC, the standard is:

      Directories are bold
      Links are italic

      I think there were also underlines for something, but hell if I can remember what.

      Here's some links to help you on your way:

      http://www.webservertalk.com/archive109-2004-3-1 44 589.html
      http://sunsite.nus.sg/pub/LDP/HOWTO/mini /Colour-ls

    9. Re:Oooo, religious wars!! by sp0rk173 · · Score: 2, Interesting

      Thank you, kind sir. We have both helped each other out today. Italics on links does make more sense.

    10. Re:Oooo, religious wars!! by fimbulvetr · · Score: 2, Funny

      I rarely find it useful, especially dir and executable colors, but I will tell you what has saved my ass once or twice:

      The big bold red color that tells me a symlink is broken.

      Outside of that one use, I cannot name a another time when I have thanked the color granting shell overlords.

    11. Re:Oooo, religious wars!! by Mr.+Slippery · · Score: 2, Informative
      The alternative standard (for those of us with purdy graphical terminals on our UltraSparcs) of using Bold and Italics is much easier on the eyes and conveys just as much information.

      Colors? Fonts? Feh. You kids and your fancy terminals.

      The "standard" is to alias ls to 'ls -F'. This appends * to executables, / to directories, @ to symlinks, = for sockets, and | for FIFOs. Works on any terminal.

      --
      Tom Swiss | the infamous tms | my blog
      You cannot wash away blood with blood
    12. Re:Oooo, religious wars!! by /ASCII · · Score: 3, Interesting



      Syntax highlighting can give you a great deal of information. I have written a shell called fish, that syntax highlights the commands as you are typing them. What I find useful about this is that fish colors potential errors in red. Mistyped commands, non-existant options, reading from non-existing files and loads of other errors can be identified by just glancing on your screen.

      </shameless plug>

      --
      Try out fish, the friendly interactive shell.
  4. Which shell is best for you? by Greg+Wright · · Score: 5, Informative

    I think either shell mentioned in the book is great, I personally use
    bash, mostly just because of historical reasons.

    Speaking of different shells in general. Here is a very handy list
    that can help you pick which shell is best for you. This is not meant
    to start a war over which shell is better but is just meant to help
    pick the shell that is best for you:

    http://www.unix.com/showthread.php?t=12274

    Just thought it would be helpful.

    --
    --greg Vulcan quiescent... Q: What machine shutdown with this message?
  5. What? No "Making vi Sane?" by suitepotato · · Score: 2, Funny

    Other than that omission, I will add it to my "must waste time at Borders sipping coffee and avoiding my insane family" reading list.

    --
    If my grammar and spelling are off, I am [distracted/tired/careless] (take your pick)
    1. Re:What? No "Making vi Sane?" by soupdevil · · Score: 2, Funny

      Some of us avoid our insane families by moving out of the basement and into our own apartments.

  6. Don't believe it. by Anonymous Coward · · Score: 2, Funny

    but this one goes into some litte-seen side streets and alleyways to show you the shortcuts

    Oh sure, those dark alleys might seem to be the shortest route to your destination, but really their just a quick path to a mugging.

  7. W00t! Finally! by lpangelrob2 · · Score: 4, Interesting
    A book I might just buy after reading about it on Slashdot.

    I like the Advanced Bash Scripting Guide quite a bit, but I'd also like to learn about other shells, without reading through a mountain of manpages, nor reading through webpages, and just for general interest while riding the El (not because "I need to do x!").

    1. Re:W00t! Finally! by civilizedINTENSITY · · Score: 2, Informative

      Isn't that Bourne Again SHell?

  8. What about COMMAND.COM? by Ann+Elk · · Score: 5, Funny

    Quickly runs out of the room and hides.

  9. also a good book by carnivore302 · · Score: 3, Informative

    Unix shells by example. Just enough in-depth for my taste. Full of tricks you actually will remember, and if you don't remember the exact syntax it can be quickly found in the book again (which I occasionally/often have to do... Kudos to those that can write commands with multiple single and double quotes in one go :-) ).

    The book is mostly on bash, and c-shell.

    --
    Please login to access my lawn
  10. SASH? by techfury90 · · Score: 2, Informative

    I still can't figure out why the Irix bootloader is called SASH, which means the standalone shell... it would make you think it lets you do unix like things without Irix booted, but nooooo it uses the same syntax and commands even as the PROM monitor uses, except sash can read XFS volumes unlike the boot PROM and list the contents of directories.

    --
    I'm friends with the youngest daughter of the former head of the PowerPC division of IBM you insensitive clod!
  11. Why I love zsh by winkydink · · Score: 5, Interesting

    Back in '91 or so, I lost legitamate source-code access to ksh. bash was around but, at the time, it only had emacs-like command-line editing. I then discovered that a bunch of folks in the CS dept at Princeton were developing a new (to me anyway) metashell called zsh that was, for the most part a ksh workalike with vi-like command-line editing.

    One thing I have never been able to do successfully is to emulate the old ksh's ability to hit Esc-Esc on the command line and get popped into vi with the last command in the history file ready to be edited. It was such a boon to rapid development of shell scripts. Then again, I don't do as much shell scripting these days, so its probably no great loss.

    If anybody knows how to do this, esc-esc thing in zsh and can tell me, I'd be really grateful.

    --

    "I'd rather be a lightning rod than a seismometer." -Ken Kesey

    1. Re:Why I love zsh by Anonymous Coward · · Score: 4, Interesting

      Whilst I am no expert on ZSH, the following seems to do what you ask for

      bindkey -s '\e\e' 'fc\n'

      This is with a freshly updated debian 'unstable' system,

      zsh --version
      zsh 4.3.0-dev-1 (i686-pc-linux-gnu)

    2. Re:Why I love zsh by Alakaboo · · Score: 2, Informative

      In bash (and ksh) I hit ESC to enter command mode, then v to bring up (v)isual mode. Perhaps zsh is similar? HTH.

  12. Best shell by Kaa · · Score: 4, Funny
    My shell is
    perl -d -e 42
    --

    Kaa
    Kaa's Law: In any sufficiently large group of people most are idiots.
  13. shell war game..... by ocularDeathRay · · Score: 2, Funny

    bash$ Shall we play a game? bash$ How about Global Thermonuclear War

    --
    Obama is a twitter sock puppet
  14. New ideas by /ASCII · · Score: 2, Informative



    I think both zsh and bash could use a redesign. The syntax is crufty, with stupid variable assignment syntax ('foo = bar' is not the same thing as 'foo=bar'? '$foo' is not the same thing as `$foo` or "$foo"?), insufficient tab-completion support and very few features enabled by default.
    I have written a shell called fish. It has lots of new features. Check it out.

    </shameless plug>

    --
    Try out fish, the friendly interactive shell.
    1. Re:New ideas by /ASCII · · Score: 2, Informative

      No, you are wrong.

      In bash:

      $foo gives you the contents of variables foo as a list of space-separated strings. "foo bar" becomes "foo" and "bar".

      "$foo" gives you the contents of variable foo as a single variable. "foo bar" remains "foo bar".

      '$foo' gives you the string "$foo".

      `$foo` executes the command pointed to by the variable foo in a subshell. If foo has the value "ls" executes the "ls" command.

      In fish:

      $foo gives you the contents of variable foo as a single string, "foo bar" remains "foo bar".

      "$foo" and '$foo' both give you the string "$foo".

      And those are all the quoting styles allowed in fish.

      --
      Try out fish, the friendly interactive shell.
    2. Re:New ideas by lawpoop · · Score: 4, Funny

      A shell called fish? Can you call it shellfish for short? Did you make it GPL? If so, thanks for not being selfish with shellfish!

      --
      Computers are useless. They can only give you answers.
      -- Pablo Picasso
  15. Unicode by olafc · · Score: 5, Insightful

    I like zsh and use it a lot, but there is just one big feature still missing: proper unicode support.

    I know they started working on it in february, at least more actively then before. So it will come eventually. Once the internals are done they will move on to the line editor.

    Untill then, it still "eats" my prompt when backslashing over multibyte characters... :(

  16. Suggestion by metamatic · · Score: 4, Funny

    I cordially propose that the default prompt of your new shell be changed to

    }><(([@>

    for reasons which should be obvious.

    --
    GCHQ Quantum Insert installed. If only our tongues were made of glass, how much more careful we would be when we speak
  17. Re:bash rules! by pilkul · · Score: 2, Informative
    The flip-side is that students who don't know how to use the different shells are probably screwed anyway once they get into the real world. Go figure.

    If I'm thrown into a shell I don't like, I just type "bash"<enter> and that's that. If the students can't figure that one out, they really are screwed.

  18. Wicked! by veg_all · · Score: 4, Informative
    . After the binding, typing CTRL-x t puts the string 'March 2004' onto the command line, and moves the cursor under the '2' so you can insert the day of the month.

    What a timesaver! I start so many commands with
    $ March 2004
    In all seriousness, though, I'd like to stick my suggestion in here: Wicked Cool Shell Scripts is a charming little read and the scripts are all on line!

    --
    grammar-lesson free since 1999. (rescinded - 2005)
  19. Mr Old fashioned by mrbooze · · Score: 3, Insightful

    Every shell script I write can run in a non-dynamic bourne shell. With all my years of supporting dozens of different flavors of unix, I grew accustomed to never assuming what shells may or may not be available on a given system.

    #!/bin/sh 4 LIFE!

    1. Re:Mr Old fashioned by PigleT · · Score: 2, Insightful

      For scripting, maybe, unless you know the script isn't going to go off your box. For actual command-line interaction, give me zsh or give me death. "Compatibility" is no reason not to learn what fun *can* be had.

      --
      ~Tim
      --
      .|` Clouds cross the black moonlight,
      Rushing on down to the circle of the turn
    2. Re:Mr Old fashioned by /ASCII · · Score: 2, Interesting

      While I acknowledge the existance of shell hell, I don't think your example is evidence of it. You highlight the fact the some versions of test allow for '==' to mean the same thing as '='. This, I'm sure, was done to prevent a common, harmless user error from causing trouble. All you have to do to steer clear of such problems is not to rely on 'test' to support non-standard options.

      There is however another problem related to the one you outline above. bash, zsh and probably most of the other shells in your example do not use the standard test command, but do instead use a builtin with the same name. I think including dozens of standard unix commands such as kill, test and printf in the shell binary as builtins is a design mistake. It breaks the unix tradition of doing one thing and doing it well. At least bash has had some trouble with fd redirections and builtins. A bug in a builtin may cause the entire shell to crash. When the user switches shells or invokes a program directly, and not through the shell, he/she may find out that the programs syntax has changed!

      For the reasons outlined above, I think the only commands that the shell should provide as builtins are the ones that can not be implemented as a separate command, such as cd, exit and if.

      --
      Try out fish, the friendly interactive shell.
  20. zsh plug: Recursive file completion by gidds · · Score: 4, Interesting
    A good chart, but it omits the single feature which for me puts zsh head and shoulders over the others: recursive globbing.

    I used to find myself using find a lot -- mostly for handling files in subdirectories, and/or for selecting files based on metadata of various kinds. But zsh makes find almost totally unnecessary: it has a simple but very powerful syntax which extends the simple '*' and '?' file completion to allow selection by size/date/type/&c, exclusion lists, user-specified ordering, and most usefully it can select files in subdirectories too. And because it's right there in the shell, the results are easy to use without that awkward -exec syntax. I don't think I've used find once since switching to zsh!

    I really don't understand why it hasn't become more popular. It's free and open source; it can mimic other shells (notably, ksh), and it ships with systems such as Mac OS X.

    --

    Ceterum censeo subscriptionem esse delendam.

    1. Re:zsh plug: Recursive file completion by Sq · · Score: 2, Interesting

      "find -exec grep ..." (for example) is awkward, and it is bloody too slow (it executes command for EVERY file)

      that's why you want to use something like:
      find | xargs grep
      which will not only execute grep only once per few thousand files, but it will also work where ZSH's
      "grep ... **/*" won't -- when there are too many files (yes, UN*X has a limit on command line length, it 's just that it is somewhat bigger than DOS's 127 characters)

      while you're at it, you probably want
      find -print0 | xargs -0 grep ... (to solve the problem with those nasty whitespace characters in filenames...)

  21. $20 at bookpool by Szplug · · Score: 2, Informative
    book at bookpool.com.

    Perhaps I'm evil, not supporting bn.com but, it's a massive difference in price - cheap enough to pick up casually.

    --
    Someday we'll all be negroes
  22. Try it yourself ;-) by codergeek42 · · Score: 2, Funny
  23. IPython by ultrabot · · Score: 4, Interesting

    Those shopping for a shell, and with a taste for Python, would do well to check out ipython.

    In the 'pysh' mode, it acts as a fully functional system shell, even on Windows. Bash and friends suck on windows, but IPython truly shines there. It really makes the command prompt use of Windows feasible, with bash like filename completion etc. Being able to extend it with python functions (as opposed to separate scripts) is a killer feature as well.

    I've actually replaced the command prompt launchers on my KDE desktop with 'ipython -p pysh' launchers.

    --
    Save your wrists today - switch to Dvorak
  24. Re:ZSH rocks except for one feature by opk · · Score: 2, Interesting
    All you should need is the following in your ~/.zshrc
    HISTFILE=~/.zshistory
    SAVEHIST=500
    HISTSIZE=500
    If that doesn't help let me know. Or mail zsh-users. I've never had a problem with it and it has always worked well.
    I also set these options:
    setopt EXTENDED_HISTORY
    setopt HIST_EXPIRE_DUPS_FIRST
    setopt HIST_FIND_NO_DUPS
    setopt HIST_REDUCE_BLANKS
    setopt HIST_SAVE_NO_DUPS
    setopt SHARE_HISTORY
    The last of those allows history so be shared between concurrently running shells.
  25. Re:ZSH rocks except for one misfeature by /ASCII · · Score: 2, Insightful

    I think this is a good illustration of one of the major problems with zsh. To get a decent history file with all the cool features, you have to add 9 different commands to you configuration file.

    To get a goob tab completion, with a good pager, etc, you need another boatload of commands. You will also want to redefine some keyboard shortcuts to more powerful versions of the same commands and install a few nifty add-ons.

    So suddenly you have spent a few hours creating a 100+ lines long configuration file just to enable some of the common zsh options. If dedicating your life to tweaking your shell sounds like fun, then go ahead. But most people have better things to do with their time.

    --
    Try out fish, the friendly interactive shell.