home | list info | list archive | date index | thread index

[OCLUG-Tech] some weird shell parameter substitution constructs

  • Subject: [OCLUG-Tech] some weird shell parameter substitution constructs
  • From: "Robert P. J. Day" <rpjday [ at ] crashcourse [ dot ] ca>
  • Date: Sun, 8 Apr 2018 04:21:08 -0400 (EDT)
  not sure if i'm reading a shell script properly ... here's a script
for downloading and installing kubernetes:

  https://get.kubernetes.io/

but it seems to be doing strange (messy) things with parameter
substitution.

  first, there's this bit of weirdness partway through the script:

    if [[ -x ./cluster/get-kube-binaries.sh ]]; then
      # Make sure to use the same download URL in get-kube-binaries.sh
      KUBERNETES_RELEASE_URL="${KUBERNETES_RELEASE_URL}" \
        ./cluster/get-kube-binaries.sh
    fi

  i'm unsure of the value of assigning a variable to itself; is there
something subtle or tricky about that assignment that isn't obvious?

  then there are a number of constructs like this:

    if [[ -n "${KUBERNETES_SKIP_CREATE_CLUSTER-}" ]]; then
      exit 0
    fi

  AIUI, the whole point of the construct ${VAR-default}

is if VAR is not set, then the value of "default" will be used. so
what does it mean to say:

    ${VAR-}

that is, to have the empty string as the default value? is this some
magic that i am unaware of?

  and there's this near the top:

KUBERNETES_RELEASE_URL="${KUBERNETES_RELEASE_URL:-https://dl.k8s.io}";

ok, that will admittedly set that variable to a value if it does not
already have a value, but i've always done that this way:

  : ${VAR:=defaultvalue}

the script clearly works, but it seems to have been written by someone
who didn't quite grasp proper parameter substitution. thoughts?

rday