I'm parsing the output of `ls` and something went wrong

Solution 1:

This illustrates why you should NEVER rely on parsing the output of the ls command to iterate over directory contents - use a simple shell glob instead e.g. to match any filename ending in a digit

for f in $mydir/*[0-9]
do
    if [ -e $mydir/$1 ]
    .
    .
done

As to how it is producing the line cp -i driver.sh 32/, you can see that when you grep for digits in the output of ls -la, it is matching the total: 32K line as well as the wanted directory names.

Solution 2:

This illustrates why you should NEVER rely on parsing the output of the ls command to iterate over directory contents - use a simple find command:

find . -maxdepth 1 -type d --regex './[0-9]+$' -exec cp $1 {} \;