Book Image

Learning Shell Scripting with Zsh

By : Gaston Festari
Book Image

Learning Shell Scripting with Zsh

By: Gaston Festari

Overview of this book

Table of Contents (13 chapters)

Function definitions

Finally, we will turn our attention to compsys, zsh's completion system. This is one of the most complex parts of the shell for users and developers alike. Before we dive into compsys, however, we need to make a quick stop and meet an actual function in the wild.


As usual, you can learn more about compsys via the manpages. Of particular interest are man zshcompsys and man zshcompwid.

Here's what one of these looks like:

hi() {
print 'Hello, world'

Here, we have defined the hi function, which is how we'll call it again later when we need it. This will, in turn, print Hello, world every time we use it. So let's get to it, shall we?

Open your terminal emulator of choice, and type the following (one line at a time):

% hi() {
function> print 'Hello, World!'
function> }

Notice how zsh realized this was indeed a function we were trying to define and immediately used the continuation prompt (function>), allowing you to continue working on it? How nice of zsh to wait...