JIYIK CN >

Current Location:Home > Learning > OPERATING SYSTEM >

Solve R command not found on Bash (or Cygwin)

Author:JIYIK Last Updated:2025/03/21 Views:

Commands may sometimes behave differently than you expect, even if it appears that you have done everything right, such as in the case of bash: '\r': command not foundor similar error messages, for example syntax error near unexpected token $'\r'.

When you see this type of error message, you have to 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 resolve or prevent it from happening again.


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

Different operating systems behave differently, Windows is usually different. As mentioned before, the presence of the \r character is what causes this error.

\rHas to do with newline characters, which are managed differently in different operating systems.

Windows uses a different line-ending style than Unix and modern Macs, which use \n (LF - Line Feed) and \r (CR - Carriage Return) as line-ending characters and older Macs (macOS before OS X).

Unix, Linux, and modern Macs use only \n to start a new line, which is important when processing text in a terminal.

So if you're on a Windows PC (perhaps running Cygwin, Git Bash, or WSL2 - Windows Subsystem for Linux), the script you're trying to run most likely contains CRLF characters (\r\n as line breaks) that you might have seen in Windows' default Notepad editor.

Windows Notepad

With this and the possibility that you are creating a script or file, you are running commands using a Windows based editor, you have Windows style (or CRLF) line endings and need to get rid of the CRLF characters.

A simple solution is to change the file format to Unix character format LF using an editor like Notepad++ so that it will not display the following command.

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

Open the script you are working on using Notepad++ on your operating system (Windows or Mac) and change the file format setting by double clicking the Windows (CR LF)/Macintosh (CR) tab and changing to Unix (LF).

notepadpluspluss

Afterwards, run the same commands on the newly configured script and you should have no problems.

Let's recreate the same problem using a simple script file with the following content.

#!/bin/bash

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

Expected output (no errors):

Testing the Carriage Return Character
Behavior
Wow! It works

We saved the script file with the default Windows style ending, and the results of running the script file are as follows.

script.sh

Although it still outputs the echo statement, it gives us '\r': command not found . Let's save the same file in Unix (LF) format.

script unix

Now, the error message does not exist.


Solution to bash: '\r': command not found With dos2unix

When you are in a terminal (or Cygwin terminal), you may not want to use a text editor to make some line ending changes. This is where dos2unix comes into play with our bash: '\r': command not found problem.

With dos2unix you can convert files in DOS (Windows) or Mac-format line endings - plain text to script - to UNIX-format line endings.

A simple dos2unix command on our script.sh will resolve our error message.

dos2unix.exe script.sh

You may get a command not found error, especially if you are using the Cygwin terminal on Windows and the package has not been installed.

Linux comes with most typical distributions, but if yours doesn’t, 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 the existing installation. After that you should see the "Select a Package" screen and a search bar.

select packages

Using the search bar, search for dos2unixand double-click on the Skip value (you should see a version number). After that, click Next and complete the installation.

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

dos2unix

As you can see, the command changes the script.sh file to the Unix format as shown in 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 convert text available through the Linux terminal (including Cygwin). With it, we can filter out the trailing \rcharacters without causing problems in our script.

The following command can help remove \rthe characters and eliminate error messages we encounter when using certain RegEx expressions and utilities.

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

In your case you can replace script.sh with the file name.

Let us try the sed command on the CRLF style script script.sh.

sed

Now, we are free from $ '\r': command not foundthe trouble of .

With these three alternatives, you can easily avoid this problem by saving your script files in Unix (LF) format as you work. However, if it is something we can prevent and just a file we happen to have access to, we can use dos2unix or the sed command to handle the error.

For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.

Article URL:

Related Articles

How to decompress x.tar.xz format files under Linux

Publish Date:2025/04/08 Views:186 Category:OPERATING SYSTEM

A lot of software found today is in the tar.xz format, which is a lossless data compression file format that uses the LZMA compression algorithm. Like gzip and bzip2, it supports multiple file compression, but the convention is not to compr

Summary of vim common commands

Publish Date:2025/04/08 Views:115 Category:OPERATING SYSTEM

In Linux, the best editor should be vim. However, the complex commands behind vim's powerful functions also make us daunted. Of course, these commands do not need to be memorized by rote. As long as you practice using vim more, you can reme

Detailed explanation of command return value $? in Linux

Publish Date:2025/04/08 Views:58 Category:OPERATING SYSTEM

? is a special variable. This variable represents the return value of the previous command. That is to say, when we run certain commands, these commands will return a code after running. Generally, if the command is successfully run, the re

Common judgment formulas for Linux script shell

Publish Date:2025/04/08 Views:159 Category:OPERATING SYSTEM

In shell script programming, predicates are often used. There are two ways to use predicates, one is to use test, and the other is to use []. Let's take a look at how to use these two methods through two simple examples. Example 1 # test –

How to use the Linux file remote copy command scp

Publish Date:2025/04/08 Views:151 Category:OPERATING SYSTEM

Scp copies files between two hosts over the network, and the data is encrypted during transmission. Its underlying layer uses ssh for data transmission. And it has the same authentication mechanism and the same security level as ssh. When u

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Recommended

Tags

Scan the Code
Easier Access Tutorial