Linux commands are not always executed individually in the terminal. These commands may be compiled inside script files such as bash to be saved and executed in a certain sequence. While attempting to execute these bash files, an error can be prompted in front of the user with the statement “ ‘\r’: command not found”.
This article will elaborate on the reason for the error “’\r’: command not found” and demonstrate the solution.
Fix the Error “ ‘\r’: command not found”
There is just one major cause that will invoke this error, but there are a few different solutions to resolve this issue. This section will shed light on all of them.
Reason: Script has Windows DOS Syntax
The use of scripting files is not limited only to Linux-based OS. The scripts are also used on operating systems such as Windows. This would naturally mean that there are subtle differences in the syntax of the scripting files used in the Windows OS vs. a Linux-based OS. This is seen as the main cause for the error being prompted. The end-of-line characters used in Linux to slightly different from the ones that are utilized in the Windows operating system. To be more specific, Windows DOS uses the “\r\n” and Linux uses the “\n” line ending.
Solution 1: Use the dos2unix Tool
The most common solution to this issue is to utilize the dos2unix tool that exists for Linux systems. This tool scans through the dos script file and makes changes to the syntax. These changes will make the script suitable to be as Unix scripting files. The first step is to install this tool on your system, this can be done using the command shown below:
$ sudo apt install dos2unix
The dos2unix tool has been installed on the system. The next step is to change the file from DOS format to UNIX format. This is achieved by executing the command shown below. In our case, the file name is “sample.sh”:
$ dos2unix sample.sh
This command will convert the format of the file to fit the requirements of a Linux script file, and the error should no longer persist when you attempt to use execute the script.
Solution 2: Use the “sed” Command
The sed is a command that is used in Linux systems to search and replace strings inside a file. This will fit perfectly for our scenario since the problem is known to us that certain line endings are causing the problem. Below is the general format of a sed command:
$ sed -i 's/search/replace/g' file
Here, the “search” represents the string that needs to be searched inside the file, and the “replace” is the string that will replace it inside the “file”. Let’s look at the command that will fix the formatting issues. This command is shown below:
$ sed -i 's/\r$//' file
This command is being used to remove all of the “\r” strings from the scripting files by searching for them and then replacing that with “ ”. This will remove the Windows end-of-line command from the scripting file, and hence the error will no longer persist.
The “ ‘\r’: command not found” occurs when your file contains the DOS script syntax for line ending, but you are attempting to run the script on a Linux system. There are two methods through which this error can be resolved. The first method is to install and use the dos2unix tool to convert the file’s syntax. The second method uses the sed command to remove the String “\r” from the file. This article has provided detailed reasons for the error “’\r’: command not found” and demonstrated a list of possible solutions.
The Best Linux Tutorials Website