I am trying to write a bash function that will list each subdirectory and file in the current directory and index them in an array, then it will cd into the desired directory by accessing that index in the array. When I echo the array at index 8 it outputs 0[8]. why is it not outputting the directory name?
num=0
for dir in ./*
do
echo -n $num
echo -n ": "
echo $( basename $dir )
num=$(($num+1));
done
declare -a array=( $(for i in {0..$num}; do echo 0; done) )
for dir in ./*
do
for i in {0..$num};
do
if [ -z $array[$num] ]; then
$dir= basename $dir
$array[$num]= $num
fi
break
done
done
echo "Enter the directory number: "
read requested
cd "$array[$requested]"
{0..$num}doesn't work in bash as braces will be expanded before parameters.