<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xml:base="https://bjorn.now/" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:content="http://purl.org/rss/1.0/modules/content/"><channel><title>Bash on bjorn.now</title><link>https://bjorn.now/tags/bash/</link><description>Recent content in Bash on bjorn.now</description><generator>Hugo</generator><language>en-us</language><lastBuildDate>Thu, 09 Oct 2025 11:50:33 +0200</lastBuildDate><atom:link href="https://bjorn.now/tags/bash/feed.xml" rel="self" type="application/rss+xml"/><item><title>Don't quote regex patterns in Bash [[ ]] tests, because they'll match literally</title><link>https://bjorn.now/til/2025-10-09-dont-quote-regex-patterns-in-bash-tests-because-theyll-match-literally/?utm_medium=feed</link><pubDate>Thu, 09 Oct 2025 11:50:33 +0200</pubDate><guid>https://bjorn.now/til/2025-10-09-dont-quote-regex-patterns-in-bash-tests-because-theyll-match-literally/</guid><description>Don’t quote regex patterns in Bash [[ ]] tests, because they’ll match literally.
So the below won’t match because it’s looking for a literal $ in the string, instead of matching at the end of the string:
file="https://example.com/blog-post/index.html" [[ "$file" =~ "/index.html$" ]] &amp;&amp; echo MATCH But this will:
file="https://example.com/blog-post/index.html" [[ "$file" =~ /index\.html$ ]] &amp;&amp; echo MATCH Note: [[ ]] is a Bash extension of the test command that supports regexes (among other things). The [ ] is the POSIX standard test (see man test), so [[ ]] features may not work in other shells or in limited shells like ash or sh.</description><content:encoded><![CDATA[<p>Don&rsquo;t quote regex patterns in Bash <code>[[ ]]</code> <a href="https://www.gnu.org/software/bash/manual/html_node/Conditional-Constructs.html#index-_005b_005b">tests</a>, because they&rsquo;ll match literally.</p>
<p>So the below won&rsquo;t match because it&rsquo;s looking for a literal <code>$</code> in the string,
instead of matching at the end of the string:</p>
<div class=" copy-enabled">
  <div class="highlight"><pre tabindex="0" class="chroma"><code class="language-shell" data-lang="shell"><span class="line"><span class="cl"><span class="nv">file</span><span class="o">=</span><span class="s2">&#34;https://example.com/blog-post/index.html&#34;</span>
</span></span><span class="line"><span class="cl"><span class="o">[[</span> <span class="s2">&#34;</span><span class="nv">$file</span><span class="s2">&#34;</span> <span class="o">=</span>~ <span class="s2">&#34;/index.html</span>$<span class="s2">&#34;</span> <span class="o">]]</span> <span class="o">&amp;&amp;</span> <span class="nb">echo</span> MATCH</span></span></code></pre></div>
</div>
<p>But this will:</p>
<div class=" copy-enabled">
  <div class="highlight"><pre tabindex="0" class="chroma"><code class="language-shell" data-lang="shell"><span class="line"><span class="cl"><span class="nv">file</span><span class="o">=</span><span class="s2">&#34;https://example.com/blog-post/index.html&#34;</span>
</span></span><span class="line"><span class="cl"><span class="o">[[</span> <span class="s2">&#34;</span><span class="nv">$file</span><span class="s2">&#34;</span> <span class="o">=</span>~ /index<span class="se">\.</span>html$ <span class="o">]]</span> <span class="o">&amp;&amp;</span> <span class="nb">echo</span> MATCH</span></span></code></pre></div>
</div>
<p><strong>Note:</strong> <code>[[ ]]</code> is a Bash extension of the test command that supports regexes (among other things).
The <code>[ ]</code> is the POSIX standard test (see <code>man test</code>),
so <code>[[ ]]</code> features may not work in other shells or in limited shells like ash or sh.</p>
<p>I learned this while removing <code>index.html</code> from file paths to bust Cloudflare&rsquo;s cache for directory listings/pretty URLs.
I then used <a href="https://www.gnu.org/software/bash/manual/html_node/Shell-Parameter-Expansion.html">parameter expansion</a> to replace the match with an empty string, so directory URLs are also purged:</p>
<div class=" copy-enabled">
  <div class="highlight"><pre tabindex="0" class="chroma"><code class="language-shell" data-lang="shell"><span class="line"><span class="cl"><span class="nb">echo</span> <span class="si">${</span><span class="nv">file</span><span class="p">/index.html/</span><span class="si">}</span></span></span></code></pre></div>
</div>
]]></content:encoded></item></channel></rss>