Convert line endings with dos2unix
Windows uses CRLF (\r\n) line endings; Linux uses LF (\n). When a .sh script makes the trip from a Windows editor to a Pi-class device, bash sees the carriage return as part of the shebang — #!/bin/bash\r — and refuses to run the script. The error you’ll see is misleading:
-bash: ./your_script.sh: /bin/bash^M: bad interpreter: No such file or directoryIt looks like the interpreter is missing. It isn’t — the line endings are wrong. dos2unix strips the \r and the script runs.
This bites you almost every time you copy a script from a Windows machine to the FR201 over a USB stick or via SCP.
Install dos2unix
Section titled “Install dos2unix”sudo apt-get install dos2unix -yConvert a single script
Section titled “Convert a single script”dos2unix your_script.shsudo chmod +x your_script.sh./your_script.shConvert many files at once
Section titled “Convert many files at once”If you’ve just copied a directory of scripts onto the device, use find to convert in bulk.
Convert every file in the current directory (non-recursive):
find . -maxdepth 1 -type f -exec dos2unix {} \;Convert only shell scripts, recursively:
find . -type f -name "*.sh" -exec dos2unix {} \;Convert every file under the current tree:
find . -type f -exec dos2unix {} \;Then make the shell scripts executable in one pass:
find . -type f -name "*.sh" -exec chmod +x {} \;Automate the USB-drive copy
Section titled “Automate the USB-drive copy”If you’re constantly hand-carrying scripts on a USB stick, automate the convert-and-copy step. The script below mounts a USB drive, converts every file from CRLF to LF, copies the tree to a destination directory, and marks the shell scripts executable.
See the Mount a USB drive guide for how to set up the mount point.
-
Create the helper script in your home directory:
Create the helper script sudo nano /home/pi/convert-files-on-copy.sh -
Paste in the contents below. Adjust
USB_MOUNTandDESTto match your setup:/home/pi/convert-files-on-copy.sh #!/bin/bash# Converts CRLF to LF on every file on a mounted USB drive,# copies the tree to DEST, and marks shell scripts executable.USB_MOUNT="/media/usbdrive"DEST="/home/pi/scripts"if ! mountpoint -q "$USB_MOUNT"; thenecho "USB drive not mounted at $USB_MOUNT. Mount it first."exit 1fiecho "Converting files from Windows format..."find "$USB_MOUNT" -type f -exec dos2unix {} \;echo "Copying files to $DEST..."mkdir -p "$DEST"cp -r "$USB_MOUNT"/* "$DEST"/echo "Marking shell scripts executable..."find "$DEST" -type f -name "*.sh" -exec chmod +x {} \;echo "Done." -
Make it executable:
Mark the helper executable sudo chmod +x /home/pi/convert-files-on-copy.sh -
Run it whenever you’ve plugged in a fresh USB stick:
Run the helper /home/pi/convert-files-on-copy.sh