0

What is ${DEBUG- } means in below yaml config? There is a dash - and space in ${}, how to use this?

version: '3.3'
services:
  localstack:
    image: localstack/localstack:latest
    ports:
      - "4566:4566"
      - "8080:8080"
    environment:
      - SERVICES=dynamodb,cognito,ssm,sqs,es,s3,ses
      - DEFAULT_REGION=ap-northeast-1
      - LOCALSTACK_API_KEY=${LOCALSTACK_API_KEY}
      - DEBUG=${DEBUG- }
      - DATA_DIR=${DATA_DIR- }
      - LAMBDA_EXECUTOR=${LAMBDA_EXECUTOR- }
      - KINESIS_ERROR_PROBABILITY=${KINESIS_ERROR_PROBABILITY- }
      - DOCKER_HOST=unix:///var/run/docker.sock
    volumes:
      - "${TMPDIR:-/tmp/localstack}:/tmp/localstack"
      - "/var/run/docker.sock:/var/run/docker.sock"
kinjia
  • 41
  • 5

1 Answers1

1

Not sure about YAML, but in bash, the expression ${VARNAME-DEFAULT} means: Replace by the contents of the variable VARNAME if it exists, otherwise use DEFAULT

Here's an example:

$ echo "Hello ${PL-Earth}"
Hello Earth
$ PL=Neptune
$ echo "Hello ${PL-Earth}"
Hello Neptune
$ unset PL
$ echo "Hello ${PL-Earth}"
Hello Earth

When I look at your yaml file, it looks very similar: If DEBUG is set, use whatever is set, otherwise use a single space, etc. This is actually a common usage, adding the line

DEBUG=${DEBUG- }

ensures that a variable with the name DEBUG exists, even if it contains only a space.

Well, as for how to use it, set the corresponding environment variables before you execute whichever program uses this yaml file.

$ export DEBUG='-g -O0 -fbacktrace -Wall'
$ <Call to program>

I don't know much about docker, but there is almost certainly a way to set environment variables in a docker configuration.

chw21
  • 7,970
  • 1
  • 16
  • 31
  • 1
    Notice the subtle difference between `${VARNAME-DEFAULT}` and `${VARNAME:-DEFAULT}`. The latter means _Replace by the contents of the variable `VARNAME` if it exists **or is empty**, otherwise use `DEFAULT`_ – kvantour Jan 08 '21 at 04:44
  • @chw21 Thank you , I get understand, this is bash variable Parameter Expansion – kinjia Jan 08 '21 at 05:02