When I try to pass an array as parameter and add a value to it, it end's up as being empty at the end.
How would I add value to passed array? I would make the array global, but I will be passing different arrays, so it must be passed to this function as parameter.
#!/bin/bash
base='/home'
declare -a files_new
declare index
arrayit() {
#$1 = path
#$2 = array
dir=($1/*)
arr=$2
for directory in "${dir[@]}"
do
if [[ -d $directory ]]
then
arrayit $directory $arr
else
arr[$index++]=$directory
fi
done
}
index=0
arrayit $base $files_new
for file in "${files_new[@]}"
do
echo "File: $file"
done
$2.arris not an array; you are passing the first element ($files_newis equivalent to${files_new[0]}) as a regular string, and assigning that toarr.${files_new[@]}be considered as passing entire array?bash4, you can throw outarrayitaltogether and just setfiles_new=( "$base"/**/*/ )to get a recursive list of directory names. (Withshopt -s globstarto enable use of**.)