'Bash loop not closing with Screen session

As part of a larger script I am pulling data in a screen session and have been unsuccessful at stopping the while loop even after the screen session has ended. The screen session running the loop look like this.

screen -S watchDownload -dm bash -c "
            while true; do
                /usr/bin/rsync -Ppart \"$src\" \"$dest\"
                sleep 2  
            done
            " 

When I want to end the loop if I end the screen session like this it terminates the session but the files continue to sync over. If I look up bash in Activity Monitor it is still running and I can successfully close it from there. I don’t want to killall bash however because there are other sessions running also.

screen -S watchDownload -X quit

I also tried to break the loop but it seems assignment goes to the default shell and not the bash env

 screen -S watchDownload -dm bash -c "
            scan=\"on\"
            while [ \"\$scan\" == \"on\" ]; do
                /usr/bin/rsync -Ppart \"$src\" \"$dest\"
                sleep 2  
            done
            " 

I tried



screen -S watchDownload -X scan=‘off’

And

screen -S watchDownload -X bash -c “scan=‘off’”


Sources

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

Source: Stack Overflow

Solution Source