0

I was reading the elasticsearch documentation on Elastic Stack on Docker with TLS.

I came across this line in the healthcheck block:

curl --cacert $CERT_PATH/ca/ca.crt -s https://localhost:9200 >/dev/null
if [[ $$? == 52 ]]; then
   echo 0
else
   echo 1
fi

The script uses the CA cert in the specified directory to verify the peer.

I know that $? is the exit status of the most recent foreground pipeline (curl in this case). My question is why are there 2 $ in the [[...]] test command construct. I tried removing the 1 $, but then Docker complains about an invalid interpolation format for the health check option ...

Does the 1st $ serve some specific purpose in docker-compose files or is $$? in itself some special shell variables?

Link to the full docker-compose.yml on elastic's Github repo. The above line corresponds to line 38 in the linked docker-compose.yml.

Algo7
  • 2,122
  • 1
  • 8
  • 19
  • Why did you try to remove a `$` ? Does the docker-compose fail when you execute it ? – Aserre Mar 29 '21 at 07:12
  • I removed the 1st `$` just to see if it will work. Because the test is to see if `curl` returns status code 52. What I don't get is that if `$?` means the exit status of the previous program why does the script use `$$?` And yes, docker-compose failed to start when I removed the 1st `$` – Algo7 Mar 29 '21 at 07:18

1 Answers1

1

I am no expert in docker nor elasticsearch. Perhaps this has less to do with bash than with make.

To get a $ in a makefile it must be escaped with an additional $. So to get $? in a makefile you need $$?.

On the other hand in bash $$ gets you the pid of the current process, so of the bash.

Jona Engel
  • 369
  • 1
  • 13
  • This seems to make a lot of sense. However, why isn't \ used for escaping? Also, what shell do you use when you're running your makefile. Do you use `[...]` or `[[...]]` for the test construct? Just want to iron out all other possibilities. – Algo7 Mar 29 '21 at 07:25
  • 2
    There is no Makefile here. You can use either `[` or `[[` depending on what you want; see https://stackoverflow.com/questions/3427872/whats-the-difference-between-and-in-bash – tripleee Mar 29 '21 at 07:35
  • 1
    on up for tripleee. While there is no Makefile there, the mechanism behind it might be inspired by one. If you run a script like that, I try to stick with POSIX and `sh`. – Jona Engel Mar 29 '21 at 09:12