How do I fix "$'\r': command not found" errors running Bash scripts in WSL?

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?


Inside WSL:

sudo apt-get install dos2unix

Then,

dos2unix [file]

Full documentation:

man dos2unix

Saved my day, hope it helps.


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.