Goodbye, World! Basic Array Iteration in bash using The Walking Dead character names.

Category: Software and Systems Engineering
Author: Sean O'Donnell
Wed, Oct. 22nd, 2014 @ 14:30:37 (MDT)

The following example is a simple "Hello, World!" type of bash array construct and iteration process. There are many other possible array constructs and iteration processes, but this one focuses on a simple array of (variable) strings which should be used to execute a series of commands autonomously during an iteration process. In this example, we'll welcome our favorite Zombie-Apocolypse characters to their new world famous zombie soap opera series.

#!/usr/bin/env bash
zombies=(Rick Carl Glen Maggy Daryl Carol Michone Tyreese Bob);
for i in ${zombies[@]}; do
echo "Welcome to the Walking Dead, ${i}.";
done

The output of which would result in:

Welcome to the Walking Dead, Rick.
Welcome to the Walking Dead, Carl.
Welcome to the Walking Dead, Glen.
Welcome to the Walking Dead, Maggy.
Welcome to the Walking Dead, Daryl.
Welcome to the Walking Dead, Carol.
Welcome to the Walking Dead, Michone.
Welcome to the Walking Dead, Tyreese.
Welcome to the Walking Dead, Bob.

It's so simple, even a zombie can do it!


Copyleft (<) 1998-2018 www.seanodonnell.com