{"id":301,"date":"2021-12-05T09:06:47","date_gmt":"2021-12-05T17:06:47","guid":{"rendered":"https:\/\/junsun.net\/wordpress\/?p=301"},"modified":"2023-12-19T11:18:17","modified_gmt":"2023-12-19T19:18:17","slug":"save-bash-output-to-a-file-automatically","status":"publish","type":"post","link":"https:\/\/junsun.net\/wordpress\/2021\/12\/save-bash-output-to-a-file-automatically\/","title":{"rendered":"Save Bash Output to A File &#8211; Automatically"},"content":{"rendered":"\n<p>I often have a need to save the output a bash file to another file, e.g., log file.  I know I could use redirection &#8220;&gt;&#8221; or &#8220;tee&#8221;.  But I would have to type it from command line.  This post talk about doing it from within the script itself.<\/p>\n\n\n\n<p>Google search does not yield much meaningful results.  To save the hassle for me and potentially others.  Here is the straight no-BS code.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#!\/bin\/bash\n\nreadonly file=\"output\"\nexec 1> >(tee $file)\nexec 2>&amp;1\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>I often have a need to save the output a bash file to another file, e.g., log file. I know I could use redirection &#8220;&gt;&#8221; or &#8220;tee&#8221;. But I would have to type it from command line. This post talk about doing it from within the script itself. Google search does not yield much meaningful &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/junsun.net\/wordpress\/2021\/12\/save-bash-output-to-a-file-automatically\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Save Bash Output to A File &#8211; Automatically&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"spay_email":""},"categories":[3],"tags":[71,72],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/junsun.net\/wordpress\/wp-json\/wp\/v2\/posts\/301"}],"collection":[{"href":"https:\/\/junsun.net\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/junsun.net\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/junsun.net\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/junsun.net\/wordpress\/wp-json\/wp\/v2\/comments?post=301"}],"version-history":[{"count":3,"href":"https:\/\/junsun.net\/wordpress\/wp-json\/wp\/v2\/posts\/301\/revisions"}],"predecessor-version":[{"id":400,"href":"https:\/\/junsun.net\/wordpress\/wp-json\/wp\/v2\/posts\/301\/revisions\/400"}],"wp:attachment":[{"href":"https:\/\/junsun.net\/wordpress\/wp-json\/wp\/v2\/media?parent=301"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/junsun.net\/wordpress\/wp-json\/wp\/v2\/categories?post=301"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/junsun.net\/wordpress\/wp-json\/wp\/v2\/tags?post=301"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}