Indexed arrays and for loops
Keep a list of names intact while numbering each item.
Arrays preserve a list
Bash 3.2 supports indexed arrays. services=('api gateway' 'auth' 'backup') stores three values, one of which contains a space. ${services[0]} reads the first element. Always use "${services[@]}" when you want each element to become a separate argument; "${services[*]}" joins the list into one value.
Associative arrays arrived in Bash 4 and are outside this course’s required subset. Likewise, do not depend on mapfile to read a list. A simple loop is explicit and works with the Bash included in macOS.
Repeat one understandable operation
for service in "${services[@]}"; do ...; done visits the stored values. Keep a counter with index=$((index + 1)). That assignment succeeds even when the new arithmetic value is zero, unlike an arithmetic command whose exit status depends on its value.
"$@" preserves script argument boundaries in the same way. An argument containing a wildcard is data once quoted. The shell that invokes your script controls its initial splitting, so users should quote names with spaces when calling it too.
services=('api gateway' 'auth' 'backup')
for service in "${services[@]}"; do
printf '%s\n' "$service"
done
Put it into practice.
Use all supplied arguments as service names, or default to `api gateway`, `auth`, `backup` when none are supplied. Write `services.tsv` with one `1-based index<TAB>name` row per service in its original order, no header. Preserve spaces, wildcards and Unicode. Print nothing to the terminal.
See an explained solution
if [ "$#" -eq 0 ]; then
services=('api gateway' 'auth' 'backup')
else
services=("$@")
fi
index=0
: > services.tsv
for service in "${services[@]}"; do
index=$((index + 1))
printf '%d\t%s\n' "$index" "$service" >> services.tsv
done
The indexed array preserves argument boundaries. Truncating the output before the loop makes repeated runs replace the report.