99

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. Commented Oct 7, 2020 at 20:35

2 Answers 2

131

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. Commented Sep 9, 2016 at 15:22
  • 18
    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 :-) Commented Sep 9, 2016 at 16:09
  • 4
    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
    Commented Aug 14, 2018 at 18:34
  • 6
    @StephenHarris, I think it should be "The -- stops the -z being interpreted as an options", right?
    – LRDPRDX
    Commented 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. Commented Sep 19, 2020 at 22:28
1

The set command supports options (including flags) and arguments

set [-abefhkmnptuvxBCEHPT] [-o option-name] [--] [-] [argument …]
  • options can be used to change how the command works (i.e. how it interprets filenames)
  • arguments tell commands what to do: i.e. your user-input

e.g. the set command has an option

-a: Each variable or function that is created or modified is given the export attribute and marked for export to the environment of subsequent commands.

When we execute:

$ set -a 12; echo $1
12

you can see that $1 is set to 12, but -a was consumed by the set command

If we really want to set -a as $1 (and 12 as $2), we can use -- to tell the set command to stop processing options (i.e. everything that follows is an argument)

$ set -- -a 12; echo $1 $2
-a 12

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .