x
Loading
 Loading
Hello, Guest | Login | Register

Writing Scripts for zsh

One especially impressive feature of zsh is its context-sensitive completion system. With zsh, you can use the tab key to complete file names, command flags, shell variable names, and even scripting language syntax.

Believe it or not, these “built-in” completions are simply shell functions. If you want to customize the behavior of your command line, you can edit existing completion functions or create your own. This month, we’ll look at basic zsh scripting and learn some fundamental skills as preparation for next month’s column on programming completions.

The zsh scripting language extends the Bourne shell syntax and can be quite complicated. Set aside some time to experiment and don’t underestimate the learning curve because it’s “only shell scripting.” Mastering the basic concepts will take some effort.

Globbing

Let’s start with a simple problem. Suppose you have a directory hierarchy full of text files with their execute permission bit set. How can you recursively turn off the execute permission for files, but leave the execute permissions for the directories alone? With the Bourne shell, you might do something like this:

 % find . -type f -print | xargs chmod -x 

Using zsh and globs (special strings containing wildcard characters like ?, *, etc.) the solution is simpler. zsh’s globbing abilities are good enough to make find unnecessary. We need only run this to get the same result:

 % chmod -x **/*(.) 

The leading **/ tells zsh to glob recursively. The * following that means to get everything. Finally, the glob ends in a (.), which is a glob qualifier, that restricts the matching to files.

Here’s another example (with additional glob qualifiers). Suppose an…

Please log in to view this content.

Not Yet a Member?

Register with LinuxMagazine.com and get free access to the entire archive, including:

  • Hands-on Content
  • White Papers
  • Community Features
  • And more.
Already a Member?
Log in!
Username

Password

Remember me

Forgotten your password?
Forgotten your username?
Read More
  1. Helpful Tools for Software Developers
  2. The Github Hall of Fame
  3. Book'em, Github.
  4. This Week on Github: Stupid Ruby Tricks
  5. A Veritable Scatter Shot!
Follow Linux Magazine
Rackspace