39

I'm trying to understand what this Docker entrypoint does.

It seems to me that's a very common pattern when writing Dockerfiles, but my bash skills are limited and I have no idea of all the special bash symbols kung fu.

Also, it's hard to google for "--", "$!" etc. What are these called in bash world?

To summarize, what is the line bellow trying to do?

if [ "${1#-}" != "$1" ]; then
  set -- haproxy "$@"
fi
52

The set command (when not setting options) sets the positional parameters eg

$ set a b c
$ echo $1
a
$ echo $2
b
$ echo $3
c

The -- is the standard "don't treat anything following this as an option"

The "$@" are all the existing position paramters.

So the sequence

set -- haproxy "$@"

Will put the word haproxy in front of $1 $2 etc.

eg

$ echo $1,$2,$3
a,b,c
$ set -- haproxy "$@"
$ echo $1,$2,$3,$4   
haproxy,a,b,c
  • i don't see any difference using "--" or not though. Tried here and got the same results. I'd love if you could improve the answer to highlight the difference. – Lucas Pottersky Sep 9 '16 at 15:22
  • 8
    Compare set -- -z 2 3 4 and set -z 2 3 4. The -- stops the -a being interpreted as an option. In this case it's not necessary, but it's "good practice" to get into the habit of using -- when you're note sure what is following :-) – Stephen Harris Sep 9 '16 at 16:09
  • 1
    The other main difference is that if you are sourcing a script inside of another script, using set -- $arg1 $arg2 will allow your sourced script to read the arguments, otherwise only Bash supports passing arguments directly to a sourced script (and that can be tricky to notice). – dragon788 Aug 14 '18 at 18:34

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.