1

I have a template file like this:

export const environment = {
  api: {
    url: '$API_URL',
    key: '$API_KEY',
  }
}

and I was trying to replace the actual placeholder values like this:

sed 's/$\([A-Z_]*\)/$(\1)/g' ./input.ts ./output.ts

So I wanted it to replace the placeholders with the actual values of the environment variables but when I run the above script I get:

export const environment = {
  api: {
    url: '$(API_URL)',
    key: '$(API_KEY)'
  }
}

So given these vars:

export API_URL=someurl
export API_KEY=key

I would expect the output to be:

export const environment = {
  api: {
    url: 'someurl',
    key: 'key'
  }
}

I can get this working with explicitly naming the variables:

sed -e 's|$API_URL|'$API_URL'|g
        s|$API_KEY|'$API_KEY'|g
        ./in.ts > ./out.ts

But is there a way to do this dynamically?

dagda1
  • 195

0 Answers0