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
  • 349,597
  • 67
  • 533
  • 578
Nate Reed
  • 6,761
  • 12
  • 53
  • 67
  • 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
  • 349,597
  • 67
  • 533
  • 578
Sanketh
  • 1,247
  • 11
  • 18