Skip to content
Advertisement

Getting curl: (3) URL using bad/illegal format

My bash code is simply this. I am trying to learn docker but also a newbie with bash scripting. I type in something simple like google.com for the read command but it gives me curl: (3) URL using bad/illegal format or missing URL. Anyone know what I am doing wrong?

docker exec -it missingDependencies sh -c "echo 'Input Website:'; read website; echo 'Searching..'; sleep 1; curl http://$website;"

Advertisement

Answer

Curl will give that warning when invoked like this (without a domain):

curl http://

let’s define an image that has curl.

$ cat Dockerfile 
  FROM ubuntu:latest
  RUN apt-get update
  RUN apt-get install -y curl

and assemble it:

docker build .  -t foobar

So now we can run your script.

$ docker run -it --rm foobar /bin/sh -c 
  "set -x; echo 'Input Website:'; read website; echo 'Searching..'; curl https://$website ;"

+ echo Input Website:
Input Website:
+ read website
google.com
+ echo Searching..
Searching..
+ curl https://

Solution

 docker run -it --rm foobar /bin/sh -c 
  "set -x; read -p 'which website?: ' website; echo 'Searching..'; curl https://$website;"

+ read -p which website?:  website
which website?: google.com
+ echo Searching..
Searching..
+ curl https://google.com
<HTML><HEAD><meta http-equiv="content-type" content="text/html;charset=utf-8">
<TITLE>301 Moved</TITLE></HEAD><BODY>
<H1>301 Moved</H1>
The document has moved
<A HREF="https://www.google.com/">here</A>.
</BODY></HTML>

The problem is that when you run bash -c “echo whatever $website”, the $website variable will be taken from your current environment and not from the docker environment (from read website). To counteract that the $website variable is interpolated, you could use single quotes like sh -c ‘read foo; echo $foo’, or escape the dollar sign: sh -c “read foo; echo $foo”

Advertisement