As title says, I want to add directory names into an array, but can't figure it out how. This is what I have so far:
path=some/path/in/linux
declare -a categ_array
for d in ${path}/*; do
#strip directory name of the path
dir_name=${d##*/}
#add the directory name into array
categ_array+=("${dir_name}")
done
echo ${categ_array}
This code outputs only 1 directory name (doesn't matter how many directories i have).
${categ_array}expands to only the first element of the array. You should use${categ_array[@]}instead. Also, loop can be replaced with:categ_array=("$path"/*).categ_array=( "${categ_array[@]##*/")to strip the leading directory name from each element. Or, usepushd "$path"; categ_array=(*); popdto avoid adding it to each element in the first place.