By dieheld


2014-10-02 13:54:40 8 Comments

I have a bash script that is calling a python script like so:

OUTPUT=$(python /path/path/script.py attr attr attr);

The python script will return a data list like so:

[item1, item2, item3]

How can I convert the $OUPUT variable which is a string of the return python data list into a bash array?

I'd like to read each item in bash if possible.

5 comments

@mickours 2017-02-22 16:16:16

If you want to simply create a bash list from a python list in python, you can use this one-liner:

python -c 'print(" ".join([str(elem) for elem in [1, "l", 12]]))'

It gives you directly the list:

1 l 12

Note that it requires to modify the python code instead of doing this from the external bash code.

@chepner 2014-10-02 15:45:28

I would use a short Python wrapper to convert the string into something more parseable by bash.

# Proxy for script.py output that includes some potential bash pitfalls
python_output="['a b', 'c*', 6]"

# Let Python output each element of the list as a separate line;
# the only thing this cannot handle is multi-line elements; workarounds
# are possible, but not worth getting into unless necessary. 
while read -r; do
    OUTPUT+=("$REPLY")
done < <(python -c 'import ast, sys
print "\n".join(str(x) for x in ast.literal_eval(sys.argv[1]))
' "$python_output")

# Verify that each element was added to the array unscathed.
for e in "${OUTPUT[@]}"; do
    echo "$e"
done

In bash 4, you can use the readarray command to replace the while loop:

readarray -t OUTPUT < <(python -c ... )

@Cyrus 2014-10-02 14:11:35

Add () and | tr -d '[],':

OUTPUT=($(python /path/path/script.py attr attr attr | tr -d '[],'))

echo ${OUTPUT[0]}
echo ${OUTPUT[1]}
echo ${OUTPUT[2]}
echo ${OUTPUT[@]}

Output:

item1
item2
item3
item1 item2 item3

@dieheld 2014-10-02 14:37:22

Genius, thank you, this is a decent solution for me!

@glenn jackman 2014-10-02 14:56:03

+1. Things will get more complicated if the array elements contain whitespace.

@Cyrus 2014-10-02 15:54:45

@glennjackman: yes, this solution is not suitable for items with whitespaces.

@Cyrus 2018-06-05 03:47:37

With bash >= 4.0 this is suitable for items with whitespaces: mapfile -t OUTPUT < <(python /path/path/script.py attr attr attr | sed 's/[][]//g;s/, /\n/g')

@l0b0 2014-10-02 14:13:29

Bash declares arrays like this:

foo=(bar baz ban)

To convert space separated command output to an array you can therefore do this:

foo=($(my_command))

And to convert a list to a space separated string is very easy in Python:

' '.join(my_array)

If you print that instead of the list itself you can therefore trivially convert it to an array.

@alasimpara 2014-10-02 14:11:17

You can make your script.py print a string that separates each item with spaces, which Bash will convert to an array, or you can use Bash to convert the return value of the python script into the format you want.

If you chose to print a string from your script.py you can use the following python code:

returnList = [1, 2, 3]
returnStr = ''
for item in returnList:
    returnStr += str(item)+' '
print(returnStr)

In this case, the output of the following bash script:

OUTPUT=$(python /path/to/script.py)
echo $OUTPUT
for i in $OUTPUT;
do
    echo $i
done

is:

1 2 3
1
2
3

Hope this helps you.

Related Questions

Sponsored Content

38 Answered Questions

[SOLVED] How to make a flat list out of list of lists?

29 Answered Questions

[SOLVED] For-each over an array in JavaScript?

35 Answered Questions

[SOLVED] Create ArrayList from array

52 Answered Questions

[SOLVED] Getting the source directory of a Bash script from within

  • 2008-09-12 20:39:56
  • Jiaaro
  • 1357089 View
  • 4156 Score
  • 52 Answer
  • Tags:   bash directory

28 Answered Questions

[SOLVED] Finding the index of an item given a list containing it in Python

  • 2008-10-07 01:39:38
  • Eugene M
  • 2934865 View
  • 2433 Score
  • 28 Answer
  • Tags:   python list

69 Answered Questions

[SOLVED] How do I remove a particular element from an array in JavaScript?

  • 2011-04-23 22:17:18
  • Walker
  • 5311812 View
  • 6617 Score
  • 69 Answer
  • Tags:   javascript arrays

33 Answered Questions

[SOLVED] How do I check if a list is empty?

  • 2008-09-10 06:20:11
  • Ray Vega
  • 1955843 View
  • 2968 Score
  • 33 Answer
  • Tags:   python list

30 Answered Questions

[SOLVED] How to append something to an array?

24 Answered Questions

[SOLVED] Difference between append vs. extend list methods in Python

17 Answered Questions

[SOLVED] How do I tell if a regular file does not exist in Bash?

  • 2009-03-12 14:48:43
  • Bill the Lizard
  • 2099012 View
  • 2704 Score
  • 17 Answer
  • Tags:   bash file-io scripting

Sponsored Content