cosmo0 cosmo0 - 1 year ago 114
Bash Question

Are shell scripts sensitive to encoding and line endings?

I am making a NW.js app on Mac, and want to run the app in dev mode by double-clicking on an icon. First step, I'm trying to make my shell script work.

Using VSCode on Windows (I wanted to gain time), I have created a

file at the root of my project, containing this:


cd "src"
npm install

cd ..
./tools/nwjs-sdk-v0.17.3-osx-x64/ "src" &

but I get this output:

sh ./run-nw

: command not found

: No such file or directory

: command not found

: No such file or directory

Usage: npm

where is one of: (snip commands list)

(snip npm help)

npm@3.10.3 /usr/local/lib/node_modules/npm

: command not found

: No such file or directory

: command not found

I really don't understand:

  • it seems that it takes empty lines as commands. In my editor (VSCode) I have tried to replace
    (in case the
    creates problems) but it changes nothing.

  • it seems that it doesn't find the folders (with or without the
    instruction), or maybe it doesn't know about the
    command ?

  • it seems that it doesn't understand the
    argument to

  • the part that really weirds me out, is that it still runs the app (if I did a
    npm install

Not able to make it work properly, and suspecting something weird with the file itself, I created a new one directly on the Mac, using vim this time. I entered the exact same instructions, and... now it works without any issue.

A diff on the two files reveals exactly zero difference.

What can be the difference ? What can make the first script not work ? How can I find out ?


Following the accepted answer's recommandations, after the wrong line endings came back, I checked multiple things. It turns out that since I copied my
from my Windows machine, I had
, so every time I modified the bash file under Windows, it re-set the line endings to

So, in addition to running dos2unix (which you will have to install using Homebrew on mac), if you're using Git, check your config.

Answer Source

Yes. Bash scripts are sensitive to line-endings. They should have Unix-style line-endings, i.e., each line is terminated with a Line Feed character (decimal 10, hex 0A in ASCII).

Mac line endings

With Mac-style line endings, each line is terminated with a Carriage Return (decimal 13, hex 0D in ASCII). If a script file was saved with Mac line endings, Bash would only see one long line like so:

#!/bin/bash^M^Mcd "src"^Mnpm install^M^Mcd ..^M./tools/nwjs-sdk-v0.17.3-osx-x64/ "src" &^M

Since this single long line begins with an octothorpe (#), Bash treats the line (and the whole file) as a single comment.

Note: I’ve used caret notation to represent non-printing characters, i.e., ^M is used to to represent the Carriage Return characters (represented as \r in other contexts); this is the same technique used by cat -v and Vim.

DOS/Windows line endings

With Windows or DOS-style line endings , each line is terminated with a Carriage Return followed by a Line Feed character. If a script file was saved with Windows line endings, Bash sees the file as

cd "src"^M
npm install^M
cd ..^M
./tools/nwjs-sdk-v0.17.3-osx-x64/ "src" &^M

In this case, the carriage return (^M or \r) is not treated as whitespace. Bash interprets the first line after the shebang (consisting of a single carriage return character) as the name of a command/program to run.

  • Since there is no command named ^M, it prints : command not found
  • Since there is no directory named "src"^M (or src^M), it prints : No such file or directory
  • It passes install^M instead of install as an argument to npm which causes npm to complain.


The solution is to convert the file to Unix line endings.

  1. This can be done using the dos2unix program:

    dos2unix run-nw
  2. Or open the file in Vim and run the following command before saving:

    :set fileformat=unix

Note: with the Bash port for Cygwin, there’s a custom igncr option that can be set to ignore the Carriage Return in line endings (since many users use native Windows programs to edit text files).

Useful utilities

The file utility is useful for quickly seeing which line endings are used in a text file. Here’s what it prints for for each file type:

  • Unix line endings: Bourne-Again shell script, ASCII text executable
  • Mac line endings: Bourne-Again shell script, ASCII text executable, with CR line terminators
  • DOS line endings: Bourne-Again shell script, ASCII text executable, with CRLF line terminators

The dos2unix utility is very useful for converting text files between Unix, Mac and DOS line endings.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download