Software & AppsOperating SystemLinux

How To Fix “$’\r’: command not found” Errors in WSL Bash Scripts

Ubuntu 16

In the world of Windows Subsystem for Linux (WSL), one common hiccup that developers often encounter is the “$’\r’: command not found” error. This error typically arises when running Bash scripts in WSL and can be traced back to the presence of Windows-style line endings in the script file. This situation frequently occurs if the file was edited or downloaded using a Windows text editor.

This article will guide you through several methods to fix this issue, ensuring your Bash scripts run smoothly in the WSL environment.

Quick Answer

To fix the "$’\r’: command not found" error in WSL Bash scripts, you can use the dos2unix command to convert Windows-style line endings to Unix-style line endings. Another method is to use the sed command to replace the carriage return character with nothing. Additionally, updating package lists, changing line endings in a text editor, configuring Git, or using Vim can also help resolve this error.

Understanding the Issue

In Windows, line endings are denoted using the carriage return and line feed characters (CRLF), while Unix-based systems like Linux only use the line feed character (LF). When you run a Bash script in WSL that has been edited or created in Windows, the extra carriage return character can lead to the “$’\r’: command not found” error.

Method 1: Using dos2unix Command

The dos2unix command is a simple and effective tool to convert Windows-style line endings (CRLF) to Unix-style (LF).

To install dos2unix, open your WSL terminal and run the following command:

sudo apt-get install dos2unix

Once installed, you can convert your Bash script file using the command:

dos2unix [file]

Replace [file] with the name of your Bash script.

If you are using PowerShell, you can execute the same command as follows:

wsl dos2unix [file]

For more information on dos2unix, you can refer to its documentation using the man dos2unix command.

Method 2: Using sed Command

The sed (stream editor) command is another powerful tool that can be used to replace Windows-style line endings with Unix-style line endings.

To use sed for this purpose, run the following command:

sed -i 's/\r$//' [file]

Here, -i stands for in-place editing, s is for substitute, \r is the carriage return character, and $ denotes the end of a line. Replace [file] with the name of your Bash script.

Method 3: Updating Package Lists

If you encounter the error “Unable to locate package dos2unix” when trying to install dos2unix, it means your package lists are outdated. To update them, run the following command:

sudo apt-get update

After updating the package lists, you should be able to install dos2unix without any issues.

Method 4: Changing Line Endings in Text Editor

If you are using a text editor within WSL, such as VS Code, you can change the line ending setting from “CRLF” to “LF” to ensure Unix-style line endings. This setting is usually found in the bottom-right corner of the editor.

Method 5: Configuring Git

If you are using Git to download files, you can configure it to checkout specific files with LF line endings. To do this, create a .gitattributes file in the Git root directory and add the following line:

*.sh text eol=lf

This line tells Git to checkout all .sh files with LF line endings.

Method 6: Using Vim

If you have Vim installed, you can use it to set the file format to Unix and save the changes. Run the following command:

vim $filename +"set ff=unix" +wq

Here, ff stands for file format, unix sets the file format to Unix, and wq saves the changes and quits Vim.

Conclusion

By following the methods outlined in this article, you should be able to resolve the “$’\r’: command not found” error in WSL Bash scripts. To avoid this issue in the future, it is recommended to download or edit files within the WSL environment to ensure consistent Unix-style line endings. Happy coding!

What is WSL?

WSL stands for Windows Subsystem for Linux. It is a compatibility layer in Windows that allows users to run Linux command-line tools and utilities directly on Windows.

How do I install WSL?

To install WSL, open PowerShell as an administrator and run the following command: wsl --install. This command will enable the necessary components and install the latest version of WSL.

Can I run Bash scripts in WSL?

Yes, you can run Bash scripts in WSL. WSL provides a full-fledged Linux environment, allowing you to execute Bash scripts just like you would in a Linux system.

What causes the “$’\r’: command not found” error in WSL Bash scripts?

The "$’\r’: command not found" error occurs when there are Windows-style line endings (CRLF) in the Bash script file. These line endings are not recognized by the Bash interpreter in WSL, resulting in the error.

How can I convert Windows-style line endings to Unix-style line endings?

There are several methods to convert Windows-style line endings to Unix-style line endings. You can use tools like dos2unix or sed to perform the conversion. Alternatively, you can change the line ending settings in a text editor or configure Git to checkout files with the desired line endings.

What should I do if I encounter the “Unable to locate package dos2unix” error?

If you encounter the "Unable to locate package dos2unix" error when trying to install dos2unix, it means your package lists are outdated. You can update them by running the command sudo apt-get update. After updating the package lists, you should be able to install dos2unix without any issues.

How can I change the line ending setting in a text editor?

The process of changing the line ending setting may vary depending on the text editor you are using. In editors like VS Code, you can find the line ending setting in the bottom-right corner. Click on it and select "LF" (Unix-style line endings) to change the setting.

How do I configure Git to checkout files with LF line endings?

To configure Git to checkout files with LF line endings, create a .gitattributes file in the Git root directory and add the following line: *.sh text eol=lf. This tells Git to checkout all .sh files with LF line endings.

Can I use Vim to fix the line ending issue?

Yes, if you have Vim installed, you can use it to set the file format to Unix and save the changes. Run the command vim $filename +"set ff=unix" +wq, replacing $filename with the name of your file. This will set the file format to Unix and save the changes.

How can I avoid the line ending issue in the future?

To avoid the line ending issue in the future, it is recommended to download or edit files within the WSL environment itself. This ensures that the files have consistent Unix-style line endings, preventing the "$’\r’: command not found" error.

Leave a Comment

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