0

I have a simple bash script that is using a config.ini file that looks like this:

[a]
b=1
c=2
...

In the bash script, I sometimes do eval "$(cat config_file | ./print_to_env_variables.py)" where the python script is this:

config = ConfigParser()
config.readfp(sys.stdin)
for sction in config.sections():
    print('declare -A {}'.format(sction))
    for k, v in config.items(sction):
        print("{}[{}]={}'.format(sction, k ,v))

Now, what this script does, is to add all sections into env variables, allowing me to use them from bash like this: echo "${a[b]}"

This only works if I do it like:

  1. eval...
  2. check the variable

but if I:

  1. call a function that does the eval (less code dup)
  2. check...

The variable does not exist.

Why is that?

CIsForCookies
  • 12,097
  • 11
  • 59
  • 124

1 Answers1

2

declare in a shell function declares a local variable that doesn't exist outside the function. To declare a global variable, you must add the -g switch. See help declare or man bash.

choroba
  • 231,213
  • 25
  • 204
  • 289