0

I have a string like ${REPOSITORY}/company/api:${API_VERSION}. $REPOSITORY and $API_VERSION are shell variables.

$ echo ${DATA_API_VERSION}
latest
$ echo ${REPOSITORY}
com.company.repo

I want to get the interpolated string that shows the values of these variables and assign it to another variable.

This is what I get:

$ echo "$image"
${REPOSITORY}/company/api:${API_VERSION}

I want this:

com.company.repo/company/api:latest
John Kugelman
  • 330,190
  • 66
  • 504
  • 555
Nate Reed
  • 6,353
  • 10
  • 50
  • 64
  • Check that you’re using double quotes: `image="${REPOSITORY}/company/api:${API_VERSION}"`. I reproduced your example and got the result you want. – oneastok Sep 16 '19 at 20:52

1 Answers1

1

You could use sed to search and replace the two variables.

#!/bin/bash

DATA_API_VERSION="latest"
REPOSITORY="com.company.repo"

image='${REPOSITORY}/company/api:${DATA_API_VERSION}'
sed -e "
    s/\${REPOSITORY}/$REPOSITORY/g
    s/\${DATA_API_VERSION}/$DATA_API_VERSION/g
" <<< "$image"
John Kugelman
  • 330,190
  • 66
  • 504
  • 555
Sanketh
  • 1,199
  • 10
  • 18