snipt

Ctrl+h for KB shortcuts

Bash

Ways to redirect bash script output to a file from within a script.

1
2
3
4
5
6
# Redirect a script's stdout and stderr to script.log
exec 1> >(tee -a ${0##*/}.log) 2>&1

# Redirect a qsub job to a named output file
# Also want no -o and both -j and -M options
exec 1> >(tee -a $JOB_NAME.o$JOB_IB) 2>&1
https://snipt.net/embed/f8e856faf3984476b0e3298d36664ecc/
/raw/f8e856faf3984476b0e3298d36664ecc/
f8e856faf3984476b0e3298d36664ecc
bash
Bash
6
2019-06-21T14:33:55
True
False
False
/api/public/snipt/27877/
ways-to-redirect-bash-script-output-to-a-file-from-within-a-script
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><a href="#L-1">1</a> <a href="#L-2">2</a> <a href="#L-3">3</a> <a href="#L-4">4</a> <a href="#L-5">5</a> <a href="#L-6">6</a></pre></div></td><td class="code"><div class="highlight"><pre><span></span><span id="L-1"><a name="L-1"></a><span class="c1"># Redirect a script&#39;s stdout and stderr to script.log</span> </span><span id="L-2"><a name="L-2"></a><span class="nb">exec</span> 1&gt; &gt;<span class="o">(</span>tee -a <span class="si">${</span><span class="nv">0</span><span class="p">##*/</span><span class="si">}</span>.log<span class="o">)</span> 2&gt;<span class="p">&amp;</span>1 </span><span id="L-3"><a name="L-3"></a> </span><span id="L-4"><a name="L-4"></a><span class="c1"># Redirect a qsub job to a named output file</span> </span><span id="L-5"><a name="L-5"></a><span class="c1"># Also want no -o and both -j and -M options</span> </span><span id="L-6"><a name="L-6"></a><span class="nb">exec</span> 1&gt; &gt;<span class="o">(</span>tee -a <span class="nv">$JOB_NAME</span>.o<span class="nv">$JOB_IB</span><span class="o">)</span> 2&gt;<span class="p">&amp;</span>1 </span></pre></div> </td></tr></table>
bash, exec, pipe, redirect, tee