<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Shell-Scripting on Nerdsid.com</title><link>https://nerdsid.com/tags/shell-scripting/</link><description>Recent content in Shell-Scripting on Nerdsid.com</description><generator>Hugo</generator><language>en-us</language><lastBuildDate>Wed, 12 Aug 2026 00:00:00 +0000</lastBuildDate><atom:link href="https://nerdsid.com/tags/shell-scripting/index.xml" rel="self" type="application/rss+xml"/><item><title>Using Shell Variables to Avoid Errors</title><link>https://nerdsid.com/posts/avoiding-repetition-errors-with-shell-variables/</link><pubDate>Wed, 12 Aug 2026 00:00:00 +0000</pubDate><guid>https://nerdsid.com/posts/avoiding-repetition-errors-with-shell-variables/</guid><description>&lt;h2 id="task">Task&lt;/h2>
&lt;p>Create two files (&lt;code>release.txt&lt;/code>, &lt;code>owner.txt&lt;/code>) under a directory &lt;code>release-packages/northwind-api&lt;/code>. The goal is to avoid typos by storing the path once in a variable.&lt;/p>
&lt;h2 id="approach">Approach&lt;/h2>
&lt;p>Store the repeated path in a shell variable (&lt;code>TARGET_DIR&lt;/code>), then reference it for both directory creation and file writes.&lt;/p>
&lt;h2 id="solution">Solution&lt;/h2>
&lt;figure class="codeblock">
 &lt;figcaption class="codeblock__header">&lt;code class="codeblock__filename" title="file.sh">file.sh&lt;/code>

 &lt;span class="codeblock__actions">&lt;/span>
&lt;/figcaption>

 &lt;div class="codeblock__body">
 &lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-bash" data-lang="bash">&lt;span class="line">&lt;span class="cl">&lt;span class="cp">#!/bin/bash
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="cp">&lt;/span>&lt;span class="nb">set&lt;/span> -euo pipefail
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="nv">TARGET_DIR&lt;/span>&lt;span class="o">=&lt;/span>release-packages/northwind-api
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">mkdir -p &lt;span class="s2">&amp;#34;&lt;/span>&lt;span class="nv">$TARGET_DIR&lt;/span>&lt;span class="s2">&amp;#34;&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="nb">echo&lt;/span> &lt;span class="s1">&amp;#39;release=2026.06.09&amp;#39;&lt;/span> &amp;gt; &lt;span class="s2">&amp;#34;&lt;/span>&lt;span class="nv">$TARGET_DIR&lt;/span>&lt;span class="s2">/release.txt&amp;#34;&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="nb">echo&lt;/span> &lt;span class="s1">&amp;#39;owner=platform&amp;#39;&lt;/span> &amp;gt; &lt;span class="s2">&amp;#34;&lt;/span>&lt;span class="nv">$TARGET_DIR&lt;/span>&lt;span class="s2">/owner.txt&amp;#34;&lt;/span>&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>
 &lt;/div>
&lt;/figure>
&lt;p>Now run the script using &lt;code>chmod +x file.sh&lt;/code>, then &lt;code>./file.sh&lt;/code>.&lt;/p>
&lt;h2 id="verification">Verification&lt;/h2>

&lt;figure class="terminal no-copy">
 &lt;header class="terminal-header">
 &lt;div class="terminal-controls">
 &lt;span class="dot dot--red">&lt;/span>
 &lt;span class="dot dot--yellow">&lt;/span>
 &lt;span class="dot dot--green">&lt;/span>
 &lt;/div>
 
 &lt;figcaption class="terminal-title">Cat file contents&lt;/figcaption>
 
 &lt;/header>
 &lt;pre>&lt;code>$ cat release-packages/northwind-api/release.txt 
release=2026.06.09
$ cat release-packages/northwind-api/owner.txt
owner=platform&lt;/code>&lt;/pre>
&lt;/figure>

&lt;p>Verification isn&amp;rsquo;t just a formality. For example, a typo in the path or quoting can fail silently (e.g. create a wrongly named file) instead of throwing an error. So always confirm output rather than trusting the script ran correctly.&lt;/p></description></item></channel></rss>