Search code examples
windowsmacosbashnewlineline-endings

newline in mac command file created in windows


I am creating a command file in windows using JavaScript activexobject.

This file will run on Mac after double click.

I am writing the file as

script.write("#!/bin/bash\r");
script.write('cd "$(dirname "$0")"\r');

The additional \r is for carriage return line ending in Mac.

But this doesn't work. The command doesn't execute just starts and over. No cd happens.

I am sure that this problem is related to line endings because when I edit the line ending (by deleting the newline and again add newline using return key) after opening the command file in Mac with TextEdit, it works.

How can I solve this in write method?


Solution

  • You should try writing '\n' instead of '\r'. Carriage returns were used in pre OSX machines only.

    If you wanna visually see the line endings you could open up vim on the Mac and type :set list. All new lines will appear like $ and carriage returns as ^M. You should only be seeing $ if this is an OSX machine.