How do I fix "$'\r': command not found" errors running Bash scripts in WSL?
Solution 1
Inside WSL:
sudo apt-get install dos2unix
Then,
dos2unix [file]
Full documentation:
man dos2unix
Saved my day, hope it helps.
Solution 2
If you are on Windows,
- Open your shell file on NotePad++
- Click on Edit on Top bar menu, then choose EOL Conversion --> Unix(LF)
- Now copy this file in your Linux system and it should run without these errors.
Solution 3
steeldriver is correct that the problem is that you have files with Windows line endings and bash
cannot run them. $'\r'
is a representation of the carriage return character (CR) that is part of traditional DOS and Windows line endings (CR LF), but which is absent in traditional Unix-style line endings (LF).
As you say, you're typing the command to attempt to run the script in bash
, but notice that the script is actually stored outside your Ubuntu (WSL) system, in your Windows download directory:
/mnt/c/Users/vincent/Downloads/vision/pytorch-CycleGAN-and-pix2pix
WSL paths that start with /mnt/c
, where c
may be any Windows drive letter, are paths that access files and directories outside the Ubuntu system. As a Windows path, that is:
C:\Users\vincent\Downloads\vision\pytorch-CycleGAN-and-pix2pix
That a file is stored in your Windows system outside the area where Ubuntu is installed does not guarantee that it uses Windows-style instead of Unix-style line endings. However, if you downloaded the files with Git in Windows, its default configuration is to give you Windows-style line endings.
The easiest way to fix the problem is really to just download the files you need inside the Ubuntu system from your bash
prompt. I recommend you fully update the Ubuntu system, then install git
in Ubuntu, as well as build-essential
which provides useful tools that you need to compile most software from source code. For Python programs, you may not need build-essential
; you can omit it if you like, but I suspect you'll end up needing it at some point.
sudo apt update && sudo apt upgrade && sudo apt install git build-essential
Then use the cd
command to go to a directory where you would like to download the software. This should be a directory within your Ubuntu system. For example, it could be your Ubuntu home directory or somewhere inside there. Once there, clone the repository from GitHub. I did it inside the src
directory that I made inside my home directory.
cd ~/src
git clone https://github.com/junyanz/pytorch-CycleGAN-and-pix2pix.git
Of course, you will most likely have to install the dependencies first. To do that, just follow all the official instructions. Do all those steps within the Ubuntu system.
Solution 4
I was using VS code within my WSL environment to edit the bash script. Changing the setting from "CRLF" to "LF" on the bottom right corner fixed it for me.
Solution 5
I ran into this issue and the solution was to configure git to checkout specific files in Linux line-ending way.
You can add a file to your git root directory name .gitattributes
which configure specific attributes for git (see full documentation).
Add the lines
# Convert to LF line endings on checkout.
*.sh text eol=lf
To configure git to checkout .sh
files with LF line ending even on Windows machines.
Related videos on Youtube
Vincent Thomas
Updated on September 18, 2022Comments
-
Vincent Thomas over 1 year
I'm getting an error in WSL while trying to use junyanz/pytorch-CycleGAN-and-pix2pix.
I followed all the install steps successfully on Windows 10 x64 using the steps at PyTORCH on Windows 10: An instructional with screenshots, then Ubuntu for Windows with GitHub desktop How to Install and Use the Linux Bash Shell on Windows 10.
But I'm blocked at the latest steps.
It's where I trying to download or train the model in Windows. Using for example the Ubuntu Linux prompt, I navigate manually to the appropriate place and type a following command like the one below (I also tried in su mode)
bash pretrained_models/download_pix2pix_model.sh facades_label2photo
I get an immediate error, always the same, similar to these:
root@Azure:/mnt/c/Users/vincent/Downloads/vision/pytorch-CycleGAN-and-pix2pix# bash pretrained_models/download_pix2pix_model.sh facades_label2photo pretrained_models/download_pix2pix_model.sh: line 2: $'\r': command not found Note: available models are edges2shoes, sat2map, and facades_label2photo pretrained_models/download_pix2pix_model.sh: line 4: $'\r': command not found ]pecified [facades_label2photo pretrained_models/download_pix2pix_model.sh: line 6: $'\r': command not found mkdir: cannot create directory ‘./checkpoints/facades_label2photo\r_pretrained\r’: No such file or directory pretrained_models/download_pix2pix_model.sh: line 10: $'\r': command not found WARNING: timestamping does nothing in combination with -O. See the manual for details. : No such file or directoryhphoto pretrained_models/download_pix2pix_model.sh: line 12: $'\r': command not found pretrained_models/download_pix2pix_model.sh: line 13: $'\r': command not found
Any idea?
-
steeldriver over 6 yearsI have no idea what you're doing, but
$'\r': command not found
strongly suggests the issue is that you have used a Windows text editor that has saved your files with DOS-style CRLF line endings - see for example DOS vs. Unix Line Endings -
Vincent Thomas over 6 yearsBut Im just typing the command directly in the Window Ubuntu command prompt. Not passing by any text editor at all...
-
wjandrea over 6 years@VincentThomas This is a text file:
pretrained_models/download_pix2pix_model.sh
-
wjandrea over 6 yearsTry the answer here: How to change Windows line-ending to Unix version. If it works, let us know and we will close your question as a duplicate.
-
Eliah Kagan over 6 years@wjandrea I don't think this is really a duplicate of How to change Windows line-ending to Unix version. The reasonable solution here is to run
git clone
within the WSL system, and not to undertake extra effort attempting to convert each file. If the goal is to install software in Ubuntu, there is no reason to download it outside its filesystem first. -
wjandrea over 6 years@EliahKagan Yes, that's a better solution.
-
peter.babic about 4 yearsIn VScode you can just change the CRLF to LF in right hand corner of the Status Bar.
-
atom88 over 3 yearsalso you can do cat -v <filename> to see the line endings as "^M" in your unix commandline. When you have fixed it with the git settings you will see the "^M" gone.
-
-
Dan Zuzevich over 4 yearsThis works. I was using VSCode and had a shell script that executes some rsync and ssh command, and ran into this issue. I installed dos2unix inside WSL like Chaim said, and executed once,
wsl dos2unix deploy.sh
from Powershell, and now, everytime I type,wsl ./deploy.sh
everything works perfectly! Thanks a lot. -
Top-Master about 4 yearsAnother alternative would be
sed -i 's/\r$//' path/to/file
-
Alexander Higgins over 3 yearsIf you get
E: Unable to locate package dos2unix
runsudo apt-get update
first. -
Floating Sunfish over 3 yearsWorked like a charm!
-
atom88 over 3 yearsalso you can do cat -v <filename> to see the line endings as "^M" in your unix commandline. When you have fixed it with the git settings you will see the "^M" gone.
-
atom88 over 3 yearsalso you can do cat -v <filename> to see the line endings as "^M" in your unix commandline. When you have fixed it with the git settings you will see the "^M" gone.
-
marcin2x4 over 3 yearsPerfect! I noticed bizzare behaviour when doing the conversion. Before this my simple
if
statement didn't see other file in the same folder (which was there). After doing your instructionif
returnedtrue
while searching my second file. -
captain-yossarian almost 3 years@Chaim Eliyah you saved a lot of hours for me
-
Chaim Eliyah almost 3 yearsNecromancy but the
sed
above would probably be safe, as all OSes except Windows have dropped usage of the character. -
McKay G over 2 yearsAlso using Find and Replace to replace \r with nothing worked for me, but this seems like a one-click version.
-
captain-yossarian over 2 yearsIf you are getting
Failed to change the permissions of temporary output file
usesudo dos2unix [file]