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?

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

4 Comments

Understood, but looking for a way to achieve natively on the script without using source command, as we dont have the luxury to use it.
Can you use eval then?
Could you please let me know the exact format to use eval here?
@devops1 sorry missed that - sure, updated

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.