0

I wanted to export variables based on the condition as:

if [[ "$1" == *"eu"* ]]; then
 export API_URL=https://xx-gateway-eu.xx.com/cccccccc
elif [[ "$1" == *"as"* ]]; then
 export API_URL=https://xx-gateway-as.xx.com/cccccccc
else
 export API_URL=https://xx-gateway-na.xx.com/cccccccc
fi

When i execute the script using "./script.sh yyy.ee.kkk.eu.dd.com", it got executed successfully but i dont see the variable got exported, echo $API_URL is empty. Could see the variable is set if i use 'source ./script.sh' and i understand the reason why its behaving like this. But, is there a way to achieve it as part of the shell script execution itself and without using source?

devops1
  • 1
  • 3

1 Answers1

1

Not as far as I know, because that execution happens in a new process. This is what the shebang line is all about in a script.

source (or . if you prefer) is what you want here really.

EDIT: or use eval if you really need to

...If you really want to avoid source, you could use eval to ensure you stay in-process:

$ 1=foo-eu- eval "$(cat script.sh)"
$ echo $API_URL
https://xx-gateway-as.xx.com/cccccccc

With the normal caveats about eval.

declension
  • 4,110
  • 22
  • 25