<?xml version="1.0" encoding="UTF-8"?><rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/">
  <channel>
    <title>Bash &amp;mdash; Little Step</title>
    <link>https://rex.writeas.com/tag:Bash</link>
    <description></description>
    <pubDate>Sat, 09 May 2026 23:26:46 +0000</pubDate>
    <item>
      <title>Easy way to redirect log in shell</title>
      <link>https://rex.writeas.com/easy-way-to-redirect-log-in-shell?pk_campaign=rss-feed</link>
      <description>&lt;![CDATA[If you write a bash script file, and want to keep the running log, what is the easy way?&#xA;&#xA;You can just echo the log messages to stderr or stdout, and add redirection arguments when calling the script.&#xA;&#xA;You can also define one log function and call it to log messages in the whole script, but it is a little complex.&#xA;&#xA;Recently, I learn a easy way to log messages for simple shell script: using exec builtin command.&#xA;&#xA;!/bin/env bash&#xA;&#xA;add this at the begnning of script&#xA;exec   /tmp/debug.log 2  &amp;1&#xA;&#xA;other commands may echo to stderr or stdout&#xA;if [ ... ]; then&#xA;    echo &#34;success&#34;&#xA;else&#xA;    echo &#34;fail&#34;&#xA;fi&#xA;&#xA;Here is the help for exec command:&#xA;&#xA;exec: exec [-cl] [-a name] [command [arguments ...]] [redirection ...]&#xA;    Replace the shell with the given command.&#xA;&#xA;    Execute COMMAND, replacing this shell with the specified program.&#xA;    ARGUMENTS become the arguments to COMMAND.  If COMMAND is not specified,&#xA;    any redirections take effect in the current shell.&#xA;&#xA;    Options:&#xA;      -a name&#x9;pass NAME as the zeroth argument to COMMAND&#xA;      -c&#x9;execute COMMAND with an empty environment&#xA;      -l&#x9;place a dash in the zeroth argument to COMMAND&#xA;&#xA;    If the command cannot be executed, a non-interactive shell exits, unless&#xA;    the shell option `execfail&#39; is set.&#xA;&#xA;    Exit Status:&#xA;    Returns success unless COMMAND is not found or a redirection error occurs.&#xA;&#xA;Check this message: If COMMAND is not specified, any redirections take effect in the current shell.&#xA;&#xA;So it is very useful for simple script just run several commands for workflow, and especially very convenient to embed script in config files.&#xA;&#xA;\#Shell #Bash #Linux]]&gt;</description>
      <content:encoded><![CDATA[<p>If you write a bash script file, and want to keep the running log, what is the easy way?</p>

<p>You can just echo the log messages to stderr or stdout, and add redirection arguments when calling the script.</p>

<p>You can also define one log function and call it to log messages in the whole script, but it is a little complex.</p>

<p>Recently, I learn a easy way to log messages for simple shell script: using <strong>exec</strong> builtin command.</p>

<pre><code class="language-shell">
# !/bin/env bash

# add this at the begnning of script
exec &gt; /tmp/debug.log 2&gt;&amp;1

# other commands may echo to stderr or stdout
if [ ... ]; then
    echo &#34;success&#34;
else
    echo &#34;fail&#34;
fi
</code></pre>

<p>Here is the help for exec command:</p>

<pre><code class="language-shell">exec: exec [-cl] [-a name] [command [arguments ...]] [redirection ...]
    Replace the shell with the given command.

    Execute COMMAND, replacing this shell with the specified program.
    ARGUMENTS become the arguments to COMMAND.  *If COMMAND is not specified,
    any redirections take effect in the current shell.*

    Options:
      -a name	pass NAME as the zeroth argument to COMMAND
      -c	execute COMMAND with an empty environment
      -l	place a dash in the zeroth argument to COMMAND

    If the command cannot be executed, a non-interactive shell exits, unless
    the shell option `execfail&#39; is set.

    Exit Status:
    Returns success unless COMMAND is not found or a redirection error occurs.
</code></pre>

<p>Check this message: <strong>If COMMAND is not specified, any redirections take effect in the current shell.</strong></p>

<p>So it is very useful for simple script just run several commands for workflow, and especially very convenient to embed script in config files.</p>

<p>#Shell <a href="https://rex.writeas.com/tag:Bash" class="hashtag" rel="nofollow"><span>#</span><span class="p-category">Bash</span></a> <a href="https://rex.writeas.com/tag:Linux" class="hashtag" rel="nofollow"><span>#</span><span class="p-category">Linux</span></a></p>
]]></content:encoded>
      <guid>https://rex.writeas.com/easy-way-to-redirect-log-in-shell</guid>
      <pubDate>Sat, 28 Mar 2020 02:25:27 +0000</pubDate>
    </item>
  </channel>
</rss>