How to verify whether a xml file is valid in sh or bash?

user2799603 picture user2799603 · Feb 28, 2014 · Viewed 13k times · Source

How to verify whether a XML file is valid in sh (preferably) or bash?

I have a file that often get corrupted and needs to be replaced while I take my time to investigate the underlying issue.

Is there any easy way of performing this task with sh or bash?

Answer

sanmiguel picture sanmiguel · Feb 28, 2014

Not directly with bash, but xmllint is fairly widely available.

xmllint --format "${xmlfile}"

This will exit with non-zero status (hint: $? in bash gets you the exit code of the last command) if the XML file is invalid.