1

Is there a way to append one file to another in a bash script without it complaining about the < > characters? I'm trying to append a file that contains some xml to another but it keeps throwing a

syntax error near unexpected token 'newline'

'<myTag>'

The operation is simply myFile.txt >> otherFile.xml

I know they're placeholder characters but I haven't been able to turn up any results on making them get interpreted as literals

  • @Nemo very true, I don't know why I thought `>>` would be an exception –  Sep 22 '14 at 19:11

1 Answers1

6

You need to use cat:

cat myfile.txt >>otherFile.xml

Otherwise, you're trying to run myfile.txt as an executable; the problem has nothing to do with the arrow-bracket characters embedded within the file's contents.


By the way -- XML documents can only legally contain a single root, and are not allowed to have content outside of that root, so your new file probably isn't valid XML.

Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
  • Ya I realize that, I have some scripting for deleting the closing tag, appending xml and then readding it. It's a wonderful hack of a script but no one else has to see it but myself so I'm okay with it. If you have any good suggestions for dealing with xml in bash I'm open to suggsetions. This is a personal script so I don't have to worry about other users. –  Sep 22 '14 at 19:07
  • Should there be an extra space? Like `cat myfile.txt >> otherFile.xml` similar to the syntax at http://stackoverflow.com/a/13181820/3767980 – PhysicalChemist Sep 22 '14 at 19:07
  • @sreya, I'm a very big fan of XMLStarlet -- http://xmlstar.sourceforge.net -- as a toolkit for manipulating XML files from bash. – Charles Duffy Sep 22 '14 at 19:08
  • @PhysicalChemist, that's a question of style, and peoples' opinions differ. Personally, I consider the additional whitespace poor form. – Charles Duffy Sep 22 '14 at 19:08
  • @sreya, ...notably, XMLStarlet contains `pyx` and `p2x` commands, which convert XML to and from a very easy-to-parse line-oriented format. – Charles Duffy Sep 22 '14 at 19:10