<?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>Shell-Scripting on bjorn.now</title><link>https://bjorn.now/tags/shell-scripting/</link><description>Recent content in Shell-Scripting 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/shell-scripting/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><item><title>gum: clean user interactions for shell scripts</title><link>https://bjorn.now/til/2025-08-20-gum-is-a-cli-tool-that-helps-your-make-your-scripts-easy-to-interact-with/?utm_medium=feed</link><pubDate>Wed, 20 Aug 2025 23:02:03 +0800</pubDate><guid>https://bjorn.now/til/2025-08-20-gum-is-a-cli-tool-that-helps-your-make-your-scripts-easy-to-interact-with/</guid><description>gum simplifies making shell scripts interactive: no more wrestling with read commands and ANSI escape codes for user input: just proper text editing, defaults, and clean UI.
Their demo:</description><content:encoded><![CDATA[<p><a href="https://github.com/charmbracelet/gum">gum</a> simplifies making shell scripts interactive: no more wrestling with read commands and ANSI escape codes for user input: just proper text editing, defaults, and clean UI.</p>
<p><a href="https://github.com/charmbracelet/gum/blob/9ab722ca4fe1e248283091626bd9fe5ecea65e1b/examples/demo.sh">Their demo</a>: <figure><img src="/img/2025/gum-demo.gif"
    alt="gum gemo">
</figure>
</p>
<p>For example, if you run:</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">gum input --width <span class="m">50</span> --header <span class="s2">&#34;Favorite author?&#34;</span> <span class="se">\
</span></span></span><span class="line"><span class="cl"><span class="se"></span> --value <span class="s2">&#34;Terry Pratchett&#34;</span></span></span></code></pre></div>
</div>
<p>It will give you a question with a sensible default that you can then change as you want, and what you wrote as you hit <code>enter</code> will be returned so your script can then use it.</p>
<p>If you instead has some options where you want to select one:</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">gum choose --header <span class="s2">&#34;Favorite book?&#34;</span> <span class="se">\
</span></span></span><span class="line"><span class="cl"><span class="se"></span> <span class="s2">&#34;Hogfather&#34;</span> <span class="s2">&#34;Thief of Time&#34;</span> <span class="s2">&#34;The Night Watch&#34;</span> <span class="s2">&#34;Small Gods&#34;</span></span></span></code></pre></div>
</div>
<p>Which gives you a selection box, you can configure to allow multiple selections, and the one you pick is then returned.</p>
<p>And one I really love, have it show a spinner to indicate that yes… something is still going on:</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">gum spin --title<span class="o">=</span><span class="s2">&#34;Counting down from 10&#34;</span> sleep <span class="m">10</span></span></span></code></pre></div>
</div>
<p>Built on <a href="https://github.com/charmbracelet/bubbletea">bubbletea</a>, a Go TUI framework, use that directly if you need these components in Go code rather than shell scripts.</p>]]></content:encoded></item><item><title>Non-interactive shells will source the file pointed at by a variable at launch</title><link>https://bjorn.now/til/2025-08-15-if-you-start-a-non-interactive-bash-shell-it-will-source-the-content-of-the-file-defined-in-bash-env-and-env-for-a-posix-shell/?utm_medium=feed</link><pubDate>Fri, 15 Aug 2025 21:19:18 +0800</pubDate><guid>https://bjorn.now/til/2025-08-15-if-you-start-a-non-interactive-bash-shell-it-will-source-the-content-of-the-file-defined-in-bash-env-and-env-for-a-posix-shell/</guid><description>If you start a non-interactive bash shell it will source the content of the file defined in BASH_ENV (and ENV for a POSIX shell).</description><content:encoded><![CDATA[<p>If you start a non-interactive bash shell it will source the content of the file defined in <code>BASH_ENV</code> (and <code>ENV</code> for a POSIX shell).</p>
<blockquote>
<p>When bash is started non-interactively, to run a shell script, for example, it looks for the variable <code>BASH_ENV</code> in the environment, expands its value if it appears there, and uses the expanded value as the name of a file to read and execute.  Bash behaves as if the following command were executed:</p>
<p><code>if [ -n &quot;$BASH_ENV&quot; ]; then . &quot;$BASH_ENV&quot;; fi</code></p>
<p>but the value of the <code>PATH</code> variable is not used to search for the file name.</p>
</blockquote>]]></content:encoded></item><item><title>Use Commas as Prefix for Your Scripts</title><link>https://bjorn.now/link/2025-08-13-use-commas-as-prefix-for-your-scripts/?utm_medium=feed</link><pubDate>Wed, 13 Aug 2025 08:28:11 +0800</pubDate><guid>https://bjorn.now/link/2025-08-13-use-commas-as-prefix-for-your-scripts/</guid><description>Commands with commas: I came across this a couple of years ago, and I’ve been using it for my new scripts since.
Debian today supports a huge number of commands; my modest Ubuntu laptop shows several thousand available:
$ apt-file search -x ‘^/usr/bin/[^/]*$’ | wc -l
21733
The solution was obviously to adjust my command names in such a way that they were still easy to type, but would never be chosen as system command names.
[…]</description><content:encoded><![CDATA[<p><a href="https://rhodesmill.org/brandon/2009/commands-with-comma/">Commands with commas</a>: I came across this a couple of years ago, and I&rsquo;ve been using it for <a href="https://github.com/gaqzi/conf/tree/master/bin">my new scripts since.</a></p>
<blockquote>
<p>Debian today supports a huge number of commands; my modest Ubuntu laptop shows several thousand available:</p>
<blockquote>
<p>$ apt-file search -x &lsquo;^/usr/bin/[^/]*$&rsquo; | wc -l<br>
21733</p>
</blockquote>
<p>The solution was obviously to adjust my command names in such a way that they were still easy to type, but would never be chosen as system command names.<br>
[…]</p>
<p><strong>There was but one character left: the simple, modest comma.</strong></p>
<p>A quick experiment revealed in a flash that the comma was exactly the character that I had been looking for! Every tool and shell that lay in arm&rsquo;s reach treated the comma as a perfectly normal and unobjectionable character in a filename.[…]</p>
<p>And, best of all, <strong>thanks to the magic of tab-completion, it became very easy to browse my entire collection of commands</strong>. […]<br>
I simply type a comma followed by tab and my list of commands appears</p>
</blockquote>
]]></content:encoded></item><item><title>/usr/bin/env handles commands with subcommands</title><link>https://bjorn.now/til/2025-08-13-usr-bin-env-with-subcommands/?utm_medium=feed</link><pubDate>Wed, 13 Aug 2025 08:21:23 +0800</pubDate><guid>https://bjorn.now/til/2025-08-13-usr-bin-env-with-subcommands/</guid><description>/usr/bin/env executes commands with flags/subcommands, not just bare executables. Which is great if you, for example, have a script/lint that’s a Python script, and it needs dependencies from a virtualenv that isn’t active when you call it.
Just put your shebang as /usr/bin/env uv run python3 and it always runs in the virtualenv, no wrapper script needed. This feels obvious in hindsight, it’s what you expect from these tools 😃</description><content:encoded><![CDATA[<p><code>/usr/bin/env</code> executes commands with flags/subcommands, not just bare executables. Which is great if you, for example, have a <code>script/lint</code> that&rsquo;s a Python script, and it needs dependencies from a virtualenv that isn&rsquo;t active when you call it.</p>
<p>Just put your shebang as <code>/usr/bin/env uv run python3</code> and it always runs in the virtualenv, no wrapper script needed. This feels obvious in hindsight, it&rsquo;s <em>what you expect</em> from these tools 😃</p>
]]></content:encoded></item><item><title>How "$@" vs $@ expands in shell</title><link>https://bjorn.now/til/2025-08-02-in-shell-scripts-if-you-do-it-will-actually-expand-correctly/?utm_medium=feed</link><pubDate>Sat, 02 Aug 2025 09:37:29 +0800</pubDate><guid>https://bjorn.now/til/2025-08-02-in-shell-scripts-if-you-do-it-will-actually-expand-correctly/</guid><description>In shell scripts if you do "$@" it will actually expand “quoted sentences” correctly, and if you just do $@ it will always unwrap them into single words, I thought that if you did "$@" it would combine all arguments into a single argument, and what it does is do what I thought $@ alone did.
I.e., with "$@" the arguments "one two" three will be 2 arguments, the first being "one two", and without it will become three arguments, all separated by space.</description><content:encoded><![CDATA[<p>In shell scripts if you do <code>&quot;$@&quot;</code> it will actually expand &ldquo;quoted sentences&rdquo; correctly, and if you just do <code>$@</code> it will <strong>always</strong> unwrap them into single words, I thought that if you did <code>&quot;$@&quot;</code> it would combine all arguments into a single argument, and what it does is do what I thought <code>$@</code> alone did.</p>
<p>I.e., with <code>&quot;$@&quot;</code> the arguments <code>&quot;one two&quot; three</code> will be 2 arguments, the first being <code>&quot;one two&quot;</code>, and without it will become three arguments, all separated by space.</p>
]]></content:encoded></item><item><title>Scripts/commands in a shell script inherits access to STDIN</title><link>https://bjorn.now/til/2025-08-02-the-scripts-inside-a-shell-script-inherit-access-to-stdin-when-you-call-them/?utm_medium=feed</link><pubDate>Sat, 02 Aug 2025 09:37:29 +0800</pubDate><guid>https://bjorn.now/til/2025-08-02-the-scripts-inside-a-shell-script-inherit-access-to-stdin-when-you-call-them/</guid><description>The scripts/commands inside a shell script inherit access to STDIN when you call them, so if you have a shells script that only has cat and you do ./script.sh &lt; script.sh then it’ll output the content of itself</description><content:encoded><![CDATA[<p>The scripts/commands inside a shell script inherit access to STDIN when you call them, so if you have a shells script that only has <code>cat</code> and you do <code>./script.sh &lt; script.sh</code> then it&rsquo;ll output the content of itself</p>
]]></content:encoded></item></channel></rss>