How to Solve R Command Not Found on Bash (Or Cygwin)

Olorunfemi Akinlua Feb 16, 2024
  1. Solve the bash: '\r': command not found With Notepad++
  2. Solve bash: '\r': command not found With dos2unix
  3. Solve bash: '\r': command not found With sed
How to Solve R Command Not Found on Bash (Or Cygwin)

Commands can sometimes behave differently than you expect even when you have seemingly done everything right, as that’s the case with bash: '\r': command not found or similar error messages such as syntax error near unexpected token $'\r'.

When you see such error messages, you must understand the error message. In this case, it has to deal with the carriage return character, \r, which instructs the terminal to move the cursor to the beginning of the line.

This article will help you understand the error message and how to solve it or prevent such from happening again.

Solve the bash: '\r': command not found With Notepad++

Different OSs behave differently, and Windows is usually the different one. As said earlier, the presence of the \r character is the cause of this error.

The \r has to do with line breaks, which are managed differently across different OS.

Unlike Unix and modern Macs, Windows uses a different newline style which makes use of the \n (LF - Line Feed) and \r (CR - Carriage Return) as newline character styles and older Macs (macOS before OS X).

Unix, Linux, and Modern Macs use only \n to start a new line, which is important when terminals process text.

Therefore, if you are a Windows PC (probably running Cygwin, Git Bash or WSL2 - Windows Subsystem for Linux), most likely the script you are trying to run contains the CRLF characters (\r\n as newline characters) which you might have seen on the default Notepad editor on Windows.

windows notepad

With this and the possibility that you created the script or file you are running a command on with a Windows-based editor, you have Windows-style (or CRLF) line endings and need to get rid of the CRLF characters.

A simple solution could be to use an editor like Notepad++ to change the file format to Unix character format LF so that it doesn’t show the following command.

bash: '\r': command not found
syntax error near unexpected token `$'\r'
`fi

Open the script you are working with using Notepad++ on your OS (Windows or Macs) and change the file format set by double clicking the Windows (CR LF)/Macintosh (CR) tab and changing to Unix (LF).

notepadpluspluss

After that, run the same command on the newly configured script, and you shouldn’t have any issues.

Let’s recreate the same issues with a simple script file containing the below.

#!/bin/bash

echo "Testing the Carriage Return Character"
echo "Behavior"
echo "Wow! It works"

The expected output (with no errors):

Testing the Carriage Return Character
Behavior
Wow! It works

We saved the script file with the default Windows-style endings, and the result of running the script file is below.

script.sh

Though it still outputs the echo statements, it gives us the '\r': command not found. Let’s save the same file with the Unix (LF) format.

script unix

Now, the error message doesn’t exist.

Solve bash: '\r': command not found With dos2unix

When you are within the terminal (or Cygwin Terminal), you probably, don’t want to go to a text editor to make some line-ending changes. That’s where dos2unix comes in with our bash: '\r': command not found problem.

With dos2unix, you can convert your files - plain text to scripts - in DOS (Windows) or Mac format line breaks to UNIX format line breaks.

A simple dos2unix command on our script.sh will solve our error messages.

dos2unix.exe script.sh

You might get a command not found, especially if you are on Cygwin Terminal on Windows and have not installed the package.

Linux comes with most of the typical distributions, but if you don’t have it, you can use the command below or follow the installation guide.

sudo apt install tofrodos

To install dos2unix on macOS, you can install MacPorts and then use the command below or follow the installation guide via Brew.

sudo port install dos2unix

To install on Cygwin, you need to click on the installation file to update your existing installation. After, you should see the Select Packages screen and a search bar.

select packages

With the search bar, search for dos2unix and double-click on the skip value (and you should see a version number). Afterward, click Next and complete the installation.

Now, let’s try the dos2unix command on the script.

dos2unix

As you can see, the command changes the script.sh file to Unix format as indicated by the command output, and when we run the same script, the error message is no longer there.

dos2unix: converting file script.sh to Unix format...

Solve bash: '\r': command not found With sed

sed is a Unix utility that helps us parse and transform text available via Linux terminals, including Cygwin. With it, we can filter out the trailing \r character without our script causing issues.

The below command can help remove the \r character and eliminate the error message we are experiencing using some RegEx expressions and the utility.

sed -i 's/\r$//' script.sh

In your case, you can replace the [script.sh](http://script.sh) with the filename.

Let’s try the sed command on our CRLF-styled script, script.sh.

sed

Now, we are free from the $ '\r': command not found.

With these three alternatives, you can easily prevent the issue by saving your script files in Unix (LF) format when working. However, if it is something we can prevent and is simply a file we happen to have access to, we can use dos2unix or sed commands to deal with the error.

Olorunfemi Akinlua avatar Olorunfemi Akinlua avatar

Olorunfemi is a lover of technology and computers. In addition, I write technology and coding content for developers and hobbyists. When not working, I learn to design, among other things.

LinkedIn

Related Article - Bash Cygwin

Related Article - Bash Error