79

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?

8
  • 6
    I 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 Oct 19, 2017 at 22:37
  • 1
    But Im just typing the command directly in the Window Ubuntu command prompt. Not passing by any text editor at all... Oct 20, 2017 at 12:08
  • 1
    @VincentThomas This is a text file: pretrained_models/download_pix2pix_model.sh
    – wjandrea
    Oct 27, 2017 at 18:02
  • 1
    Try 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.
    – wjandrea
    Oct 27, 2017 at 18:09
  • 1
    @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. Oct 27, 2017 at 18:15

6 Answers 6

96

Inside WSL:

sudo apt-get install dos2unix

Then,

dos2unix [file]

Full documentation:

man dos2unix

Saved my day, hope it helps.

6
  • 1
    This 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. Aug 17, 2019 at 23:10
  • 4
    Another alternative would be sed -i 's/\r$//' path/to/file
    – Top-Master
    Feb 4, 2020 at 12:04
  • 3
    If you get E: Unable to locate package dos2unix run sudo apt-get update first. Aug 5, 2020 at 20:21
  • 1
    @Chaim Eliyah you saved a lot of hours for me May 31, 2021 at 12:27
  • Necromancy but the sed above would probably be safe, as all OSes except Windows have dropped usage of the character. Aug 3, 2021 at 23:03
17

If you are on Windows,

  1. Open your shell file on NotePad++
  2. Click on Edit on Top bar menu, then choose EOL Conversion --> Unix(LF)
  3. Now copy this file in your Linux system and it should run without these errors.
3
  • 3
    Worked like a charm! Aug 14, 2020 at 10:15
  • Perfect! 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 instruction if returned true while searching my second file.
    – marcin2x4
    Jan 3, 2021 at 18:39
  • Also using Find and Replace to replace \r with nothing worked for me, but this seems like a one-click version.
    – McKay G
    Aug 26, 2021 at 19:35
16

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.

1
  • 1
    also 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
    Aug 20, 2020 at 21:48
7

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.

VSCode window

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.

1
  • also 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
    Aug 20, 2020 at 21:48
4

If you have vim handy, it'll fix you up quick.

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.