Array syntax
Assuming you have the directories stored in an array:
dirs=(dir1 dir2 dir3)
You can get the length of the array thusly:
echo "There are ${#dirs[@]} dirs in the current path"
You can loop through it like so:
let i=1
for dir in "${dirs[@]}"; do
echo "$((i++)) $dir"
done
And assuming you've gotten the user's answer, you can index it as follows. Remember that arrays are 0-based so the 3rd entry is index 2.
answer=2
echo "you selected ${dirs[$answer]}!"
Find
How do you get the file names into an array, anyways? It's a bit tricky. If you have find
that might be the best way:
readarray -t dirs < <(find . -maxdepth 1 -type d -printf '%P\n')
The -maxdepth 1
stops find from looking through subdirectories, -type d
tells it to find directories and skip files, and -printf '%P\n'
tells it to print the directory names without the leading ./
it normally likes to print.