In this article
Inspect the first line without running the script
Open Terminal in the folder containing your script. Replace script.sh below with its actual filename, retaining quotation marks for spaces:
head -n 1 "./script.sh" | cat -v
file "./script.sh"
These commands read the file. They do not execute its contents. If the first command displays #!/bin/sh^M, the visible ^M represents a carriage-return character at the end of that line. It is not normally two literal characters that you should search and replace by typing a caret and an M.
The file command may report “with CRLF line terminators.” Read that together with the first-line output. A script can contain mixed line endings, so one clean line does not prove that every later line is clean.
Apple’s archived cross-platform scripting guide distinguishes Unix LF endings from Windows CRLF. Its line-ending explanation remains relevant; the dated guide’s other operating-system details should not be treated as a description of every current Mac.
Why a file that exists can still fail
The first line beginning with #! names the interpreter used when you execute the script directly. In our test, adding a carriage return after /bin/sh changed the interpreter name that macOS attempted to use. The script existed, but the requested interpreter path did not.
This is why repeated chmod commands can leave the error unchanged. Execute permission and the bytes in the first line are separate properties. Apple’s Terminal guide to executable files describes changing the former. That operation does not convert the text.
Read the exact message before choosing a repair:
| What you observe | Next check |
|---|---|
An interpreter path ending in ^M | Inspect CRLF in the first line |
| A clean-looking interpreter path that does not exist | Verify the required interpreter and installation |
permission denied | Inspect access and execute permission separately |
| A command inside the script is not found | Inspect that command’s path and environment |
For a scheduled job that starts a shell successfully but cannot find a later command, see our LaunchAgent command-not-found guide. Converting line endings will not supply a missing command or fix the job’s PATH.
A controlled Mac comparison
On November 11, 2026, we tested harmless, locally created scripts on macOS 15.7.5, Apple Silicon, launching them from zsh 5.9. The working script used /bin/sh, a short if block and a printed marker. Each case changed one relevant condition.
| Fixture | Observed result |
|---|---|
| LF text with owner execute permission | Printed the marker successfully |
| Identical text converted to CRLF, still executable | bad interpreter: /bin/sh^M: no such file or directory |
| LF text naming a deliberately nonexistent interpreter | Bad-interpreter error without ^M |
| Working LF text without execute permission | permission denied |
CRLF text passed directly to /bin/sh | An unexpected-end-of-file syntax error |
| CRLF text converted into a separate LF file | Printed the marker successfully |
The explicit-shell case matters. Running sh script.sh bypasses the interpreter choice in the first line, but it still feeds the remaining text to a shell. In our fixture, carriage returns in that body broke parsing. Invoking a different shell is not a general line-ending repair, and it can introduce compatibility problems if the script expects another language.
These are observations from one Mac version and a small fixture, not a catalog of every shell’s error wording. Different shells can describe the same failed launch differently. Inspecting the underlying file helps more than matching one exact sentence from an online answer.
Convert a separate copy with the bundled tools
If your text editor offers a line-ending setting, choose LF / Unix and save a separate copy of a script you own. Preserve the original until the result is checked.
Alternatively, in macOS’s zsh or bash, the following command writes a new file using the bundled sed:
sed $'s/\r$//' "./script.sh" > "./script-lf.sh"
Choose an unused output filename first: > can overwrite an existing file. Keep the input and output names different. The command removes a carriage return at the end of each line; it is intended for CRLF text, not a universal conversion for binary files or every historical text format.
We tested this exact conversion against the fixture above. The resulting bytes matched the original LF fixture. No additional package or administrator access was needed.
Inspect the new copy before running it:
head -n 1 "./script-lf.sh" | cat -v
file "./script-lf.sh"
For a script you wrote or have reviewed and intend to execute directly, chmod u+x "./script-lf.sh" adds the owner’s execute bit. Then test the intended behavior with safe inputs. Do not use broad permissions such as chmod 777 as a substitute for understanding the error.
A script inside an installed application package or a package-manager directory belongs to that software’s installation. Prefer its documented repair or reinstall process over silently rewriting vendor files.
Prevent the same conversion from returning
If the file comes from Git, check the repository’s existing policy before changing global settings. Git’s gitattributes documentation describes a repository rule such as *.sh text eol=lf for shell-script line endings. Coordinate that change with the project; it is not an instruction to normalize every file or discard unrelated local edits.
If the first line already uses LF, inspect the actual interpreter it names. A moved environment or removed runtime can leave a valid-looking script pointing at an unavailable program. Keep that diagnosis separate from file permissions and ACL markers.
The useful repair record is small: the exact error, the original first line, the detected line endings, the change made to a copy and the result of a controlled run. It lets you explain what failed without turning a text-format problem into a broad change to the Mac.


