Home > Software design >  Why would environment variables in a bash script be overwritten using eval echo?
Why would environment variables in a bash script be overwritten using eval echo?

Time:01-13

I'm writing a bash script based on a CircleCI orb's source code, and I'm confused why a subset of environment variables in a bash script would be set to eval echo of themselves.

For example, what would be the purpose of this line?

ORB_EVAL_REPO=$(eval echo "${ORB_EVAL_REPO}")

Here is the source code:

https://circleci.com/developer/orbs/orb/circleci/aws-ecr#orb-source

As a full example, there are five environment variables set on lines 705-709, but only two of the variables are set using eval echo on lines 691-692, even though the variables are used in the same way on lines 699-702:

command: |
    #!/bin/bash
    ORB_EVAL_REGION=$(eval echo "${ORB_EVAL_REGION}") # line 691
    ORB_EVAL_REPO=$(eval echo "${ORB_EVAL_REPO}")

    if [ "$ORB_VAL_PUBLIC_REGISTRY" == "1" ]; then
        echo "set-repository-policy is not supported on public repos"
        exit 1
    else
        aws ecr set-repository-policy \
            --profile "${ORB_VAL_PROFILE_NAME}" \ # line 699
            --region "${ORB_EVAL_REGION}" \
            --repository-name "${ORB_EVAL_REPO}" \
            --policy-text "file://${ORB_VAL_REPO_POLICY_PATH}"
    fi
environment:
    ORB_EVAL_REGION: <<parameters.region>> # line 705
    ORB_EVAL_REPO: <<parameters.repo>>
    ORB_VAL_PROFILE_NAME: <<parameters.profile-name>>
    ORB_VAL_PUBLIC_REGISTRY: <<parameters.public-registry>>
    ORB_VAL_REPO_POLICY_PATH: <<parameters.repo-policy-path>>

So far, I've:

  • read the documentation for eval
  • read StackOverflow answers to questions about the use cases of eval
  • tried to infer the purpose of eval echo from the specific source code I'm reading
  • searched for "=$(eval echo " on GitHub to try to find other examples

I'm having a hard time figuring this out.

CodePudding user response:

eval echo "$var" expands variables in $var. Example:

a=" 12 "
b=' a  $a  b  '
echo "/$b/"      # prints: / a  $a  b  /
eval echo "/$b/" # prints: / a 12 b /

Side effects are trimming (removing) multiple spaces and that it may fail with special characters like | or a '.

  • Related