dos2unix

When I tried to run a shellscript on my Ubuntu server, I ran into this error:

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

This is caused by Windows-style line endings (\r\n) in your script file insert.sh. Linux expects line endings to be Unix-style (\n).

How to fix it

Install dos2unix.

sudo apt update && sudo apt upgrade -y
sudo apt install dos2unix
dos2unix insert.sh

What is dos2unix command?

The dos2unix command is a utility used in Linux/Unix systems to convert text files from DOS/Windows format (CRLF, or carriage-return + line-feed endings) to Unix/Linux format (LF, or line-feed endings).

The error above often arises when a scripting file is created or edited on a Windows system and then transferred to a Linux or Unix environment. Due to differences in line-ending formats between the two operating systems, compatibility issues can occur. This is where dos2unix proves useful—it helps convert Windows-style line endings to a format compatible with Unix-based systems, ensuring smoother execution.

Leave a Reply

Your email address will not be published. Required fields are marked *