{"id":4725,"date":"2016-07-18T18:10:49","date_gmt":"2016-07-18T18:10:49","guid":{"rendered":"http:\/\/www.garysieling.com\/blog\/?p=4725"},"modified":"2016-07-18T18:10:49","modified_gmt":"2016-07-18T18:10:49","slug":"bash-echo-text-file-contents","status":"publish","type":"post","link":"https:\/\/www.garysieling.com\/blog\/bash-echo-text-file-contents\/","title":{"rendered":"Bash &#8211; echo text and file contents"},"content":{"rendered":"<p>Sometimes it&#8217;s useful to be able prepend or append text to a file within a bash script (e.g. if you&#8217;re piping it to something else).<\/p>\n<p>Typically you might use &#8216;cat&#8217; or &#8216;echo&#8217; but these are the wrong tool for this.<\/p>\n<p>One option is to do this:<\/p>\n<pre lang=\"bash\">\nprintf '%b\\n%b' 'SET SESSION CHARACTERISTICS AS TRANSACTION READ ONLY;\\n' \"$(cat test.sql)\"\n<\/pre>\n<p>A better option is this:<\/p>\n<pre lang=\"bash\">\nsed 1s\/^\/\"stuff to insert\"\\n\/\n<\/pre>\n<p>The advantage of this is you can avoid byte order mark errors &#8211; the printf will leave the the BOM in the middle of the file and you&#8217;ll get this (at least with psql &#8211; it only strips the BOM if it&#8217;s at the beginning of the file):<\/p>\n<pre>\nERROR:  syntax error at or near \"\u00ef\u00bb\u00bf\"\nLINE 1: \u00ef\u00bb\u00bf\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Combining pieces of text in bash<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[4],"tags":[79],"aioseo_notices":[],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/posts\/4725"}],"collection":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/comments?post=4725"}],"version-history":[{"count":0,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/posts\/4725\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/media?parent=4725"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/categories?post=4725"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/tags?post=4725"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}