Arrays in shell scripts
You can use a declare
command with -a
to make an array variable. The weird thing is that declare
appears to be a world unto itself.
But for now, let's focus on the fact that it can create arrays that can be iterated over in shell scripts. Once you declare it like so:
#!/bin/bash
declare -a locations=(
"/opt/whatever"
"/usr/share/nginx/html/junk"
)
You can iterate over the array like so:
for location in "${locations[@]}"
do
"cd ${location} && touch empty-file.txt"
done
In that example, each location in the array would get an empty-file.txt
added to it.
#unix #bash #shell #command #array