<story><title>Bash Pitfalls</title><url>https://mywiki.wooledge.org/BashPitfalls</url></story><parent_chain><item><author>nikisweeting</author><text>I consider shellcheck absolutely essential if you&amp;#x27;re writing even a single line of Bash.  I also start all my scripts with this &amp;quot;unofficial bash strict mode&amp;quot; and DIR= shortcut:&lt;p&gt;&lt;pre&gt;&lt;code&gt;    #!&amp;#x2F;usr&amp;#x2F;bin&amp;#x2F;env bash
    
    ### Bash Environment Setup
    # http:&amp;#x2F;&amp;#x2F;redsymbol.net&amp;#x2F;articles&amp;#x2F;unofficial-bash-strict-mode&amp;#x2F;
    # https:&amp;#x2F;&amp;#x2F;www.gnu.org&amp;#x2F;software&amp;#x2F;bash&amp;#x2F;manual&amp;#x2F;html_node&amp;#x2F;The-Set-Builtin.html
    # set -o xtrace
    set -o errexit
    set -o errtrace
    set -o nounset
    set -o pipefail
    IFS=$&amp;#x27;\n&amp;#x27;

    DIR=&amp;quot;$( cd &amp;quot;$( dirname &amp;quot;${BASH_SOURCE[0]}&amp;quot; )&amp;quot; &amp;amp;&amp;amp; pwd )&amp;quot;
&lt;/code&gt;&lt;/pre&gt;
I have more tips&amp;#x2F;tricks here: &lt;a href=&quot;https:&amp;#x2F;&amp;#x2F;github.com&amp;#x2F;pirate&amp;#x2F;bash-utils&amp;#x2F;blob&amp;#x2F;master&amp;#x2F;util&amp;#x2F;base.sh&quot; rel=&quot;nofollow&quot;&gt;https:&amp;#x2F;&amp;#x2F;github.com&amp;#x2F;pirate&amp;#x2F;bash-utils&amp;#x2F;blob&amp;#x2F;master&amp;#x2F;util&amp;#x2F;base.s...&lt;/a&gt;</text></item></parent_chain><comment><author>abathur</author><text>I&amp;#x27;ve had a thought percolating (for many months?) but I haven&amp;#x27;t tried to phrase it and I&amp;#x27;m leery it may sound condescending (this is also more of a public address than direct response) ...&lt;p&gt;Shellcheck tends to steer us away from &amp;quot;weird&amp;quot; parts of Bash&amp;#x2F;shell and will happily suggest changing code that was correct as written. There are good reasons for this (and I still think most scripts&amp;#x2F;projects should use Shellcheck), but Bash is a weird language, and there&amp;#x27;s a lot of power&amp;#x2F;potential in some of the weird stuff Shellcheck walls off.&lt;p&gt;If I had to nail down some heuristics for working with it...:&lt;p&gt;- do lean heavily on Shellcheck for writing new code if you&amp;#x27;re unfamiliar with the language or are only writing it under duress&lt;p&gt;- don&amp;#x27;t implement its suggestions in bulk (the time you save on iterating can easily be blown later trying to debug subtle behavior changes)&lt;p&gt;- don&amp;#x27;t apply them to code you don&amp;#x27;t understand (if you have the time and interest to understand it but find yourself stuck on some un-searchable syntax, explainshell may help)&lt;p&gt;- don&amp;#x27;t adopt them without careful testing&lt;p&gt;- do take Shellcheck warnings about code that is correct-as-written as a hint to leave a comment explaining what&amp;#x27;s going on&lt;p&gt;- do leave Shellcheck off (or only use it intermittently) if you&amp;#x27;re trying to explore, play, or otherwise learn the language</text></comment>
<story><title>Bash Pitfalls</title><url>https://mywiki.wooledge.org/BashPitfalls</url></story><parent_chain><item><author>nikisweeting</author><text>I consider shellcheck absolutely essential if you&amp;#x27;re writing even a single line of Bash.  I also start all my scripts with this &amp;quot;unofficial bash strict mode&amp;quot; and DIR= shortcut:&lt;p&gt;&lt;pre&gt;&lt;code&gt;    #!&amp;#x2F;usr&amp;#x2F;bin&amp;#x2F;env bash
    
    ### Bash Environment Setup
    # http:&amp;#x2F;&amp;#x2F;redsymbol.net&amp;#x2F;articles&amp;#x2F;unofficial-bash-strict-mode&amp;#x2F;
    # https:&amp;#x2F;&amp;#x2F;www.gnu.org&amp;#x2F;software&amp;#x2F;bash&amp;#x2F;manual&amp;#x2F;html_node&amp;#x2F;The-Set-Builtin.html
    # set -o xtrace
    set -o errexit
    set -o errtrace
    set -o nounset
    set -o pipefail
    IFS=$&amp;#x27;\n&amp;#x27;

    DIR=&amp;quot;$( cd &amp;quot;$( dirname &amp;quot;${BASH_SOURCE[0]}&amp;quot; )&amp;quot; &amp;amp;&amp;amp; pwd )&amp;quot;
&lt;/code&gt;&lt;/pre&gt;
I have more tips&amp;#x2F;tricks here: &lt;a href=&quot;https:&amp;#x2F;&amp;#x2F;github.com&amp;#x2F;pirate&amp;#x2F;bash-utils&amp;#x2F;blob&amp;#x2F;master&amp;#x2F;util&amp;#x2F;base.sh&quot; rel=&quot;nofollow&quot;&gt;https:&amp;#x2F;&amp;#x2F;github.com&amp;#x2F;pirate&amp;#x2F;bash-utils&amp;#x2F;blob&amp;#x2F;master&amp;#x2F;util&amp;#x2F;base.s...&lt;/a&gt;</text></item></parent_chain><comment><author>rgrau</author><text>You&amp;#x27;ve a lot of cool stuff in that repo! I hope you don&amp;#x27;t mind me &amp;#x27;stealing&amp;#x27; some stuff for a kind of booklet I&amp;#x27;m working on with several shell tips and tricks.&lt;p&gt;&lt;a href=&quot;https:&amp;#x2F;&amp;#x2F;github.com&amp;#x2F;kidd&amp;#x2F;scripting-field-guide&amp;#x2F;&quot; rel=&quot;nofollow&quot;&gt;https:&amp;#x2F;&amp;#x2F;github.com&amp;#x2F;kidd&amp;#x2F;scripting-field-guide&amp;#x2F;&lt;/a&gt;&lt;p&gt;PS: Any comments&amp;#x2F;PRs are super welcome there too</text></comment>