A few issues with the current code:
- invalid shebang (missing leading
/)
echo $(out) should be generating an error that out is an invalid command unless ... a) OP does in fact have a command named out or b) code displayed in question is different than OP's actual script; regardless, out is being populated as an array so need a different syntax to display
- the
python/exit(ret) command appears to send its ouput to stderr, but the out=(...) call is only capturing stdout
re: python/exit(ret) result:
$ python3 ./test_return_list.py > py.stdout 2>py.stderr
==> py.stderr <==
[1, 2, 3]
==> py.stdout <==
# empty file
Making a few changes to OP's current bash script:
$ cat test_python_return_list.sh
#!/bin/bash # fix shebang
echo "check return list from python"
out=( $(python3 ./test_return_list.py 2>&1) ) # redirect stderr to stdout
echo "return from python script: "
echo "${out[@]}" # echo contents of array out[]
typeset -p out # alternative display of out[] contents
Taking for a test drive:
$ ./test_python_return_list.sh
check return list from python
return from python script:
[1, 2, 3]
declare -a out=([0]="[1," [1]="2," [2]="3]")
NOTE: while this should address the syntax issues I can't speak to whether or not this is OP's expected contents of the out[] array ...
sys.exit()should be used instead ofexit(), the latter is for humans while the former is for programs. (2) Anything passed tosys.exitis printed to standard error.