#!/bin/bash # sample-crlf.sh: a shell script saved with Windows (CRLF) line endings. # Free sample from 99helpers.com/file-types/sh # DELIBERATELY BROKEN, for testing. Every line ends in an invisible carriage # return (\r) before the newline, which shells treat as part of the line. # # ./sample-crlf.sh fails: the system looks for an interpreter called # "/bin/bash\r". Depending on the shell you see # "/bin/bash^M: bad interpreter: No such file or directory" # or "cannot execute: required file not found". # bash sample-crlf.sh runs, with symptoms: the empty line 15 gives # "$'\r': command not found", and every value picks up a \r. # Fix: dos2unix sample-crlf.sh, or sed -i 's/\r$//' sample-crlf.sh (GNU sed), # or switch the line endings to LF in your editor. name="Ada" echo "Hello, $name!" echo "The line above should read 'Hello, Ada!'. If it shows '!ello, Ada'," echo "the carriage return stored in \$name sent the cursor back to the start." printf 'Length of name: %s (Ada has 3 letters)\n' "${#name}" # a comment swallows the \r