'How to solve this error in Bash while looping elements in directory?

So here is my code

tree() {
    shopt -s nullglob 
    cd "$1" || exit
    debt=$2
    cdebt=$3
    pl="----"
    for elem in *; do
       # code
    done
    shopt -u nullglob 
}

Can anyone help me with this i haven't been able to solve it for hours Any helps or hints are greatly appreciated



Solution 1:[1]

Try bash -x script.sh to debug your code and find the wrong logicality.

You should add double quotes to around your parameter prevent that some directory or file name which included blank space will identify to more than one parameter.

In your code, you should change $elem to "$elem", $PWD to "$PWD", $dir to "$dir".

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1