Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

The following is the python one-liner I want to run as part of my bash script

python -c "from xml.dom.minidom import parse;dom = parse('/path/to/pom.xml');print [n.firstChild.data for n in dom.childNodes[0].childNodes if n.firstChild and n.tagName == 'version']"

(pom.xml is a maven POM xml file)

I want to have the result of the command be assigned to variable MVN_VER

This is my base script:

PY_GET_MVN_VERS="from xml.dom.minidom import parse;dom = parse(\'${WS}/pom.xml\')\;print [n.firstChild.data for n in dom.childNodes[0].childNodes if n.firstChild and n.tagName == \'version\']"

funcion test_mvn {

        MVN_VER=`python -c \"${PY_GET_MVN_VERS}\"`
        echo ${MVN_VERS}


However it fails to run. If I run the script with +x option, it is what I see:

++ python -c '"from' xml.dom.minidom import 'parse;dom' = 'parse(\'\''/path/to/pom.xml\'\'')\;print' '[n.firstChild.data' for n in 'dom.childNodes[0].childNodes' if n.firstChild and n.tagName == '\'\''version\'\'']"'
  File "<string>", line 1

I think it has something to do with escaping the python code. How can I escape it properly?

share|improve this question

1 Answer 1

up vote 3 down vote accepted

There is no need for escaping or moving the argument to its own variable.

But, keeping it mostly the same, the following works for me:

#!/usr/bin/env bash
PY_GET_MVN_VERS="from xml.dom.minidom import parse;dom = parse('${WS}/pom.xml');print [n.firstChild.data for n in dom.childNodes[0].childNodes if n.firstChild and n.tagName == 'version']"
function test_mvn {
        MVN_VER=$( python -c "${PY_GET_MVN_VERS}" )
        echo ${MVN_VER}

/Users/danielbeck/Desktop/pom.xml is the example minimal POM from the Maven docs:




Please throw away your code and just use mine (after adjusting WS) instead of adjusting yours until it works. You have quite a few syntax errors in there.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.