0

I have seen the following syntax in a Bash script:

#!/usr/bin/env bash

: "${REDIS_HOST:="redis"}"
: "${REDIS_PORT:="6379"}"
: "${REDIS_PASSWORD:=""}"

What is the difference to this syntax?

#!/usr/bin/env bash

REDIS_HOST="redis"
REDIS_PORT="6379"
REDIS_PASSWORD=""

Is it a simple check such that the variable gets assigned a value if and only if the value of the variable is null before assignment? Why do we need the colon at the start of the line?

Moritz Wolff
  • 436
  • 1
  • 7
  • 16
  • https://stackoverflow.com/questions/10390406/usage-of-colon-dash-in-bash – KamilCuk Dec 10 '21 at 12:19
  • The accepted answer to the linked duplicate mentions `:=` as well. – chepner Dec 10 '21 at 12:24
  • 1
    The `:` is the command being executed; assignment is a *side effect* of the expansion of the variable, so you need a context in which expansion occurs. – chepner Dec 10 '21 at 12:24
  • But why do we need the colon at the start of the line? I.e. why do we write `: "${REDIS_PASSWORD:=""}"` instead of `"${REDIS_PASSWORD:=""}"`? What does the colon at the start of the line do? The linked question doesn't answer this. – Moritz Wolff Dec 10 '21 at 12:31
  • See https://stackoverflow.com/questions/32342841/colon-at-the-beginning-of-line-in-docker-entrypoint-bash-script – Ed Morton Dec 10 '21 at 12:54
  • @MoritzLoritz That's the colon I'm referring to in my comment. Without it, the result of the expansion itself would be executed as a command. – chepner Dec 10 '21 at 13:38

0 Answers0