Module 09 of 12

Loops, Functions & Arrays

Lessons

About This Module

With variables and conditionals in place, the next step is making scripts that handle lots of things at once: every file in a folder, every line in a log, every server in a list. Loops repeat work, functions let you name a block of code and reuse it, and arrays hold a whole list of values in one variable. Together they turn a handful of commands into a tidy, reusable tool.

This module starts with the three kinds of loops: while, until and for. Then it covers a pattern you'll use constantly, reading a file line by line with while read. Next come script arguments and functions: $1, $@, $# and shift, plus local variables and return codes. The module finishes with arrays, both numbered lists and associative arrays that work like a dictionary of key-value pairs.

Build a small script of your own alongside the lessons as you watch them in order, then continue on to Networking & Remote Work.

Lessons

4 videos
01

Bash Loops: While, Until and For

A LinuxHint lesson on the three loop types in Bash: while loops that run as long as a condition is true, until loops that run until it becomes true, and for loops that step through a list of items.

02

While Loop and read: Read a File Line by Line

How to combine a while loop with the read command to process a file one line at a time, a pattern used in countless real scripts for logs, lists and config files.

03

Script Arguments, Functions, local and return

A lesson from MyWebUniversity's free Bash and Zsh scripting course covering script arguments ($1, $@, $#), shift, writing functions, keeping variables local to a function, and returning status codes.

04

Bash Arrays: Numeric and Associative

How to create and use both kinds of Bash array: numeric (indexed) arrays for ordered lists, and associative arrays that look values up by name.