0

I have an array in bash:

ar[0]=1
ar[1]=2
ar[2]=3
echo ${ar[@]}
>1 2 3
export ar
export arTest="test"

and I would like to read in python. But nothing I tried worked:

print os.environ['arTest'] => test
print os.environ['ar']     => raise KeyError(key)
print os.environ['ar[1]']  => raise KeyError(key)

How can I read in the content of the bash array ar or ${ar[@]} into Python?

Thx

PS. In fact it seems that Tom's comment is the answer to that question. It is not a duplication of Exporting an array in bash script as export ar=(1 2 3 4) has not effect on the result.

Community
  • 1
  • 1
user224637
  • 173
  • 2
  • 12
  • 1
    Have you looped through `os.environ` to see if it is there? Or just `print os.environ` – GWW Sep 12 '14 at 14:46
  • I can see arTest, but non of my arrays that I have initialized. It seems that the os.environ is not supporting arrays. – user224637 Sep 12 '14 at 14:57
  • 1
    The point is that the array isn't an environment variable in the normal sense, and so Python can't see it. You'll need to do something in bash like write it to disk, so that python can fetch it from somewhere else (not `os.environ`). It's not a problem with Python, it's just not supported by Bash itself. – Tom Dalton Sep 12 '14 at 15:00
  • I could prabably use `for a in ${!ar[*]} ; do echo $a, ${ar[$a]};done` in a system call and read from stdout? – user224637 Sep 12 '14 at 15:18
  • - it seems that is also not working but I could pipe this output into a text file before running python code... which is what I initially wanted to prevent from happening... `proc = subprocess.Popen(args="set", stdout=subprocess.PIPE, shell=True) (out, err) = proc.communicate() print "program output:", out` is also not showing any array variables... – user224637 Sep 12 '14 at 15:26

0 Answers0