82

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
1
  • 1
    Also, to provide the simple answer to "what is the line below trying to do"... this allows you to pass arguments to the main process the container is going to run. It checks "does 'haproxy' not equal 'haproxy --help' for exmaple and if so, grabs those arguments and passes them along. For instance, docker run haproxy --help will pass my "--help" to haproxy inside the container. Oct 7, 2020 at 20:35

1 Answer 1

114

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
8
  • 1
    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. Sep 9, 2016 at 15:22
  • 17
    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 :-) Sep 9, 2016 at 16:09
  • 3
    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, 2018 at 18:34
  • 4
    @StephenHarris, I think it should be "The -- stops the -z being interpreted as an options", right?
    – LRDPRDX
    Aug 15, 2020 at 6:44
  • 1
    @EricIhli Simple script; #!/bin/bash set $1 $2 $3. Run that with -c and you'll get an error similar to ./x: line 2: set: -c: invalid option which shows the command is being interpreted as options (do help set in bash and see what set -a does). But if the line said set -- $1 $2 $3 then you don't get an error and it sets the positional paramters. Sep 19, 2020 at 22:28

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.