Zsh is a Unix interactive shell with built-in scripting language that’s particularly suitable for administration of Unix and Unix-like systems. The shell is bash-like in a number of ways, making it easy to get started with for newcomers, and the scripting language has a clean syntax and powerful features that let you take control of local and remote systems alike without the language getting in your way.

In particular the string expansion, file and directory globbing and clean syntax for iterating over lists make it a good fit for systems scripting and control of multiple machines. It comes already installed in a vast number of popular Linux and BSD-based distributions, including Apple OS X. If you find it’s not installed then it’s usually just a quick package management command away.

Being bash-like, many simple bash scripts will run without issue via zsh, and those that don’t usually only require a minimal porting effort. Scripting from scratch is where its power lies though, and we’ll get into that quite a bit in this article series.

On top of the powerful scripting language and bash similarities, the interactive shell has one compelling feature for systems administrators: multi-line, multi-area prompts. Prompts in zsh, with a minimum of configuration, can reside on the left and right of the screen and span multiple lines. With full control over colours and support for syntax highlighting, zsh gives you a truly useful prompt that’s trivially context-aware, giving you at-a-glance information about the running environment you find yourself in.

If you have a multitude of systems to look after you can configure the prompt to make it acutely aware what machine you’re on, which user you currently are, and whereabouts in the directory hierarchy you find yourself. If you’ve configured it properly, issuing ‘pwd’, ‘whoami’ or ‘hostname’ should be a thing of the past.

Lastly, it’s easy to configure and there’s a wealth of information online, so learning more about zsh and its power, as you get to know it, is easy and fun. Let’s get stuck in with the scripting language with a view to working with multiple machines over SSH, and we’ll cover the prompt in a followup article.

This article assumes some familiarity with shell scripting and basic shell operation.

Introduction to scripting with zsh


Variables in zsh are as simple as they come:


For example:


Then to use it we prefix it with the $ symbol:

echo $newest_file

Don’t worry about understanding the glob syntax just yet, we’ll get to that.


Lists, sometimes called arrays in zsh references, are your most useful data structure in zsh scripting, and are generated by wrapping all or part of an evaluated expression in parentheses:


For example:

thedirs=`ls -d *(/)`


rm *(.L0)

In the first example we capture the list in a variable, and in the second we expand it into the shell so a command can work on each entry.

Iteration over lists

The syntax here is trivial:

for var (list); do statement; done;

For example:

for i (1 2 3); do echo $i; done;


for x ({1..10}); do echo $x; done;

That example, like those above when I introduced lists, show you extra built-in syntax available in zsh for doing useful things. In this instance {x..y} will generate a sequence of integers between x and y for use in your list.

Putting it all together

Putting those three simple bits of the scripting language — variables, lists and iteration — we can start to do something useful as Unix systems administrators.

Example 1 – Get the hostname for everything on a subnet:

for i ({1..254}); do ssh 192.168.1.$i -- "hostname"; done;

Example 2 – Delete all zero-byte files in the /tmp dir on three (zsh-equipped!) hosts:

for host (tom dick harry); do ssh $host -- "cd /tmp; rm *(.L0)"; done;

Example 3 – individually zip up all access logs in the last two weeks, into the home directory

cd /path/to/access/logs
for log (`ls -1 *(.mw2)`); do tar czf ~/$log.tar.gz $log; done;

You can see that with a clean syntax and a simple construct for iteration, we can do some quite powerful things in the shell. We’ve just scratched the surface of what’s possible with zsh in this article, and some of what you’ve seen is underpinned by some powerful zsh features, so we’ll go into those in a lot more detail in future articles.

Next time

Next time we’ll look at customising the prompt in a systems administrator-friendly way, and then we’ll get back into scripting and admin tasks that take advantage of zsh globs and some of the more useful expansions the shell provides.

Further reading

The zsh website:
The zsh manual:
The zsh wiki: