c33s
c33s

Reputation: 2642

How to prevent the `#!/bin/bash: not found` error when calling a script from Dockerfile

i have the following Dockerfile:

FROM debian:stretch-backports

MAINTAINER c33s

ARG DEBIAN_FRONTEND=noninteractive
COPY provision-base.sh /root/provision-base.sh
RUN chmod +x /root/provision-base.sh && /root/provision-base.sh

why do i get the following error running it:

...snip...
/root/provision-base.sh: 1: /root/provision-base.sh: #!/bin/bash: not found
...snip...

build log on dockerhub:https://hub.docker.com/r/c33s/debian/builds/bd6g8cqr2rtys9a2gguehcw/ (snip of the build log also on pastebin https://pastebin.com/6x1UK53H)

Upvotes: 4

Views: 24683

Answers (2)

Harald Nordgren
Harald Nordgren

Reputation: 12399

I suspect now that the problem is in the internal file format of your script.

It might have happened when downloading it through a web browswer. At least this is what I got after downloading /debian/provision-base.sh through Chrome on Mac OS:

$ file *
provision-base.sh: Bourne-Again shell script text executable, UTF-8 Unicode (with BOM) text
script-with-good-format.sh:   Bourne-Again shell script text executable, ASCII

and then the same error occurs for me when building the Dockerfile.

The BOM can be removed like this, which should solve the problem:

awk '{if(NR==1)sub(/^\xef\xbb\xbf/,"");print}' provision-base.sh > provision-base.sh

Upvotes: 3

Harald Nordgren
Harald Nordgren

Reputation: 12399

Bash is often in different places on different systems. Often at /bin/bash, but on this container it's located here:

 % docker run -it debian:stretch-backports
root@bb01a3db779e:/# type bash
bash is /usr/bin/bash

The env command is more predictable, and can be used to locate other programs in the shebang line. So try this in your script:

#!/usr/bin/env bash

Upvotes: 4

Related Questions