     <link rel="alternate" type="application/atom+xml" title="sebthom.de Category: IT" href="https://sebthom.de/de/category/it/feed/" />
     <link rel="alternate" type="application/atom+xml" title="sebthom.de Category: Linux" href="https://sebthom.de/de/category/it/linux/feed/" />
{"id":158,"date":"2011-01-31T23:56:53","date_gmt":"2011-01-31T22:56:53","guid":{"rendered":"http:\/\/sebthom.de\/?p=158"},"modified":"2011-09-03T19:02:12","modified_gmt":"2011-09-03T18:02:12","slug":"bash-capturing-stderr-variable","status":"publish","type":"post","link":"https:\/\/sebthom.de\/de\/158-bash-capturing-stderr-variable\/","title":{"rendered":"Bash: Capturing stderr in a variable while still printing to the console."},"content":{"rendered":"<p>Storing the stdout output of a command in a variable and displaying it is simple:<\/p>\n<pre class=\"perl\" name=\"code\">\r\nOUTPUT=$(command)\r\necho $OUTPUT\r\n<\/pre>\n<p>If you have longer running commands where you want to display stdout in realtime and also store it in a variable you can tee the output to stderr:<\/p>\n<pre class=\"perl\" name=\"code\">\r\nOUTPUT=$(command | tee \/dev\/stderr)\r\n<\/pre>\n<pre class=\"perl\" name=\"code\">\r\nOUTPUT=$(command | tee \/proc\/self\/fd\/2)\r\n<\/pre>\n<pre class=\"perl\" name=\"code\">\r\nOUTPUT=$(command | tee >(cat - >&2))\r\n<\/pre>\n<p>If you have longer running commands where you want to display stdout\/stderr in realtime and also store stderr in a variable it gets a bit complicated.<br \/>\nHowever, this can be achieved by switching stdout and stderr and then teeing the new stdout (which is stderr now) back to stderr for console output.<\/p>\n<pre class=\"perl\" name=\"code\">\r\nERROR=$(command 3>&1 1>&2 2>&3 | tee \/dev\/stderr)\r\n<\/pre>\n<pre class=\"perl\" name=\"code\">\r\nERROR=$(command 3>&1 1>&2 2>&3 | tee \/proc\/self\/fd\/2)\r\n<\/pre>\n<pre name=\"code\" class=\"bash\">\r\nERROR=$(command 3>&1 1>&2 2>&3 | tee >(cat - >&2))\r\n<\/pre>\n<p>Good reading:<br \/>\n<a href=\"http:\/\/mywiki.wooledge.org\/BashFAQ\/002\">Bash FAQ: How can I store the return value\/output of a command in a variable?<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Storing the stdout output of a command in a variable and displaying it is simple: OUTPUT=$(command) echo $OUTPUT If you have longer running commands where you want to display stdout in realtime and also store it in a variable you can tee the output to stderr: OUTPUT=$(command | tee \/dev\/stderr) OUTPUT=$(command | tee \/proc\/self\/fd\/2) OUTPUT=$(command&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[54,72],"tags":[74,99,100],"class_list":["post-158","post","type-post","status-publish","format-standard","hentry","category-it","category-linux","tag-bash","tag-stderr","tag-stdout"],"_links":{"self":[{"href":"https:\/\/sebthom.de\/de\/wp-json\/wp\/v2\/posts\/158","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sebthom.de\/de\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sebthom.de\/de\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sebthom.de\/de\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/sebthom.de\/de\/wp-json\/wp\/v2\/comments?post=158"}],"version-history":[{"count":0,"href":"https:\/\/sebthom.de\/de\/wp-json\/wp\/v2\/posts\/158\/revisions"}],"wp:attachment":[{"href":"https:\/\/sebthom.de\/de\/wp-json\/wp\/v2\/media?parent=158"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sebthom.de\/de\/wp-json\/wp\/v2\/categories?post=158"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sebthom.de\/de\/wp-json\/wp\/v2\/tags?post=158"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}