0

I have created build_docker.sh file in windows machine which has below contents.

#!/usr/bin/env bash
#This script build docker image
docker build -t hello-world .

Where I try to run above file using command sudo sh build_docker.sh
It fails with an error, Unable to prepare context: path ".\r" not found
I am using 4.2.46(2)-release Bash version

What I am missing here?

Mayur
  • 2,583
  • 16
  • 28
  • 1
    `\r` is a common escape for a literal carriage return. Check for DOS newlines. – Charles Duffy Feb 07 '19 at 10:35
  • 1
    BTW, this is the very first thing in "before asking about problematic code" in https://stackoverflow.com/tags/bash/info – Charles Duffy Feb 07 '19 at 10:36
  • I tried to edit this file to remove `\r` still facing same issue – Mayur Feb 07 '19 at 10:36
  • 1
    Possible duplicate of [How to convert DOS/Windows newline (CRLF) to Unix newline (LF) in a Bash script?](https://stackoverflow.com/q/2613800/608639) – jww Feb 07 '19 at 10:36
  • Tried to edit it *how*? If you opened it in vim, run `:set fileformat=unix` before saving. An editor that things you *want* your file to be saved in DOS format instead of UNIX format will just create CRLFs again when it saves your changes. – Charles Duffy Feb 07 '19 at 10:37
  • using buldin `vi` editor – Mayur Feb 07 '19 at 10:37
  • okay, i'll try that command – Mayur Feb 07 '19 at 10:37
  • BTW, if your script has a `#!/usr/bin/env bash` shebang, you should use `sudo bash`, not `sudo sh`, to run it (or even better, fix its permissions and just run `sudo ./yourscript`). `bash` and `sh` are two different shells, and much code that runs in bash doesn't work in sh. – Charles Duffy Feb 07 '19 at 10:39
  • @CharlesDuffy I have specified bash version – Mayur Feb 07 '19 at 10:40
  • Your `sh` is not `bash`, so the bash version you're using interactively doesn't make any difference whatsoever. Anyhow, that's not part of the problem but is a general aside re: practice issues that'll bite you later. – Charles Duffy Feb 07 '19 at 10:40
  • OKay, Got it, I'll try with Bash – Mayur Feb 07 '19 at 10:40
  • That won't fix the problem, which is still caused by CRLFs. But if you changed your script in the future to use bash-only features, it would fail when you start it with `sh`. – Charles Duffy Feb 07 '19 at 10:41
  • yes, it shows same error with bash as well – Mayur Feb 07 '19 at 10:41
  • I'll try `:set fileformat=unix` – Mayur Feb 07 '19 at 10:41
  • 1
    It is recommended use `.sh` for files in the POSIX sh language and `.bash` for bash shell scripts. – Jackdaw Oct 18 '20 at 10:04
  • I think this link https://stackoverflow.com/a/40821931/1375110 gives correct solution if using git – Satish Mar 03 '23 at 04:38

4 Answers4

3

There is an utility to convert file to unix EOL, it helped me.

dos2unix build_docker.sh
Vladimir Buskin
  • 614
  • 4
  • 8
2

… using :set fileformat=unix and sudo bash build_docker.sh solved my problem – Mayur

Mayur
  • 2,583
  • 16
  • 28
Armali
  • 18,255
  • 14
  • 57
  • 171
2

changing file name from filename.sh to .bash solved my issue.

ticktack
  • 21
  • 2
1

This will also work.

1. Open all files using vim *
2. Press "qq" to start recording
3. :set ff=unix
4. :wn
5. Press again "q" to stop recording
6. :wq!
techmagister
  • 1,188
  • 8
  • 14