Question

I want to use a Bash variable within the XPath of the program xmllint.

The following working code illustrates the overall thing I want to achieve:

    functionInformationTagContent="$(echo "${functionInformation}"\
                | xmllint --xpath '/functionInformation/description/text()' -\
                | xargs -i echo -n "{}")"

The following non working code illustrates the specific thing I want to achieve:

    functionInformationTag="${1}"
    functionInformationTagContent="$(echo "${functionInformation}"\
                | xmllint --xpath '/functionInformation/<clever code>"${functionInformationTag}"<clever code>/text()' -\
                | xargs -i echo -n "{}")"

You can see that I want to be able to use Bash variables in the XPath argument for the purposes of generalisation. Do you know how I may be able to achieve this? Could you point me in the right direction?

Thanks a million for your assistance!

Was it helpful?

Solution

Your 2nd comment is very close to right.

Recall that variables are NEVER expanded when the outermost quotes are single-quotes. You should be able to "toggle" back and forth between single and dbl quotes, as long as they are "touching" each other to keep the string all as one argument. try :

xmllint --xpath '/functionInformation/<clever code>'"${functionInformationTag}"'<clever code>/text()' -\
#--------------------------------------------------^^-------------------------^^----

IHTH

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top