Skip to content

Fixing Scripts Written in Windows

Do you get obscure errors like "\r: command not found" or "/bin/bash^M: bad interpreter" when trying to run your scripts on an SuperCloud system? If so, the most likely explanation is that the script file was created in an editor on Windows and then copied to the SuperCloud.

In this example, the file test.sh was created on a Windows system and copied to SuperCloud.

test.sh
#!/bin/bash
echo "test.sh was created on Windows"

When we try to run this script, we get an error:

$ ./test.sh
-bash: ./test.sh: /bin/bash^M: bad interpreter: No such file or directory

Text files created on DOS/Windows machines have different line endings than files created on Linux (which is the OS that the SuperCloud system runs). DOS uses carriage return and line feed (\r\n) as a line ending, while Linux uses just a line feed (\n). You need to be careful when transferring files between Windows machines and the SuperCloud system to make sure the line endings are translated properly.

From the the login node of a SuperCloud system, you can see what line endings a text file contains by running the cat -vet command on the file:

$ cat -vet test.sh
#!/bin/bash^M$
echo "test.sh was created on Windows"^M$

If you see ^M$ at the end of each line, then this file will not run on a Linux system. If you see just $ at the end of each line, then it is properly formatted to run on a Linux system.

There's a simple way to convert your DOS formatted file to Linux format - just run the command dos2unix on the file:

$ dos2unix test.sh
dos2unix: converting file test.sh to Unix format...

Verify that the line endings are Linux line endings:

$ cat -vet test.sh
#!/bin/bash$
echo "test.sh was created on Windows"$

Try running the script again:

$ ./test.sh
test.sh was created on Windows