Variables, Expansion, and Arrays

6. Variables, Expansion, and Arrays#

This chapter separates Bash data handling from general syntax: variables, parameters, expansion, command substitution, indexed arrays, and associative arrays used for counting and reporting.

  • Variables: Store shell values safely.

  • Expansion: Control when Bash substitutes values.

  • Parameters: Handle script and function inputs.

  • Arrays: Collect related values for automation.

  • Security: Avoid leaking secrets through environment, history, and PATH behavior.

Chapter Video

Associative Arrays in Bash (AKA Key-Value Dictionaries) (15:06) by Nick Janetakis

Hosted on YouTube; property of its original creator, embedded here for educational reference only.

Learning Objectives

By the end of this chapter, you will be able to:

  1. Assign and expand shell variables safely.

  2. Use positional parameters and command substitution deliberately.

  3. Apply parameter expansion to validate and transform values.

  4. Use indexed and associative arrays for scripts that count or group events.

  5. Explain common secret-leak and PATH-hijacking risks.

Chapter Flow

Glossary

#

Term

Explanation

1

Variable

A named shell value.

2

Expansion

The shell step that substitutes values or filenames.

3

Parameter

A named, positional, or special shell value.

4

Environment variable

A variable inherited by child processes.

5

Command substitution

The $(...) form that inserts command output.

6

Parameter expansion

The ${...} syntax for retrieving or transforming values.

7

Positional parameter

A script argument such as $1.

8

Indexed array

An array addressed by numbers.

9

Associative array

An array addressed by string keys.

10

PATH hijacking

Running an unintended command because lookup order was manipulated.