<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Vincent</title>
    <description>The latest articles on DEV Community by Vincent (@vlj).</description>
    <link>https://dev.to/vlj</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F3162606%2Fab3b39f3-610e-45a1-90b9-25adf0637dcd.png</url>
      <title>DEV Community: Vincent</title>
      <link>https://dev.to/vlj</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/vlj"/>
    <language>en</language>
    <item>
      <title>Sharing Variables Across TypeScript and Bash Scripts</title>
      <dc:creator>Vincent</dc:creator>
      <pubDate>Thu, 22 May 2025 14:35:16 +0000</pubDate>
      <link>https://dev.to/vlj/sharing-variables-across-typescript-and-bash-scripts-2gmj</link>
      <guid>https://dev.to/vlj/sharing-variables-across-typescript-and-bash-scripts-2gmj</guid>
      <description>&lt;h2&gt;
  
  
  Problem
&lt;/h2&gt;

&lt;p&gt;While working on a project, I came across an interesting issue. Multiple TypeScript and bash scripts needed to read from or write to a shared file. Thus, the scripts needed the file path (directly or indirectly). The question is: if the file location changes, how should I structure the code such that only one file needs to be updated (and thus reduce the risk of missing updates in various scripts)?&lt;/p&gt;

&lt;h2&gt;
  
  
  Attempt 1: Share a utils.ts file for Typescript files
&lt;/h2&gt;

&lt;p&gt;The Typescript scripts could import the file path in a &lt;code&gt;utils.ts&lt;/code&gt; and use a &lt;code&gt;getSharedFilePath&lt;/code&gt; function e.g.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;getSharedFilePath&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;utils&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;SHARED_PATH&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;getSharedFilePath&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;BUT! What about the bash scripts? They still hardcoded the path e.g.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;SHARED_PATH&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"path/to/shared/file.json"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Pros:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;✅ DRY &lt;em&gt;for Typescript files&lt;/em&gt; (but not for bash scripts)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Cons:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;❌ Violates DRY principle. Changing the file location means updating the &lt;code&gt;utils.ts&lt;/code&gt; and each bash script.&lt;/li&gt;
&lt;li&gt;⚠️ High risk of inconsistency or human error.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Attempt 2: Pass Full Path as Env Var
&lt;/h2&gt;

&lt;p&gt;Instead of setting the path in each bash script, set it as an env var like &lt;code&gt;SHARED_PATH&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="na"&gt;jobs&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;deploy&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;env&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="na"&gt;SHARED_PATH&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;path/to/shared/file.json&lt;/span&gt;
    &lt;span class="na"&gt;steps&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;run&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;npx hardhat run scripts/deploy.ts&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then in your TypeScript:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;SHARED_PATH&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;process&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;env&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And in your bash scripts, just call it e.g.:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;set&lt;/span&gt; &lt;span class="nt"&gt;-u&lt;/span&gt; &lt;span class="c"&gt;# throw error if script references an undefined variable e.g. SHARED_PATH&lt;/span&gt;
&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="nv"&gt;$SHARED_PATH&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Pros:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;✅ DRY: Scripts are isolated from changes to the file path value i.e. the file structure and file name can change without requiring script updates.&lt;/li&gt;
&lt;li&gt;✅ Keeps configurable data at high levels: env vars&lt;/li&gt;
&lt;li&gt;✅ Consistent, uniform mechanism for both TypeScript and bash scripts to access the file path&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Cons:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Here’s your list with fitting emojis prepended:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;⚠️ You lose &lt;code&gt;path.join&lt;/code&gt; safety in TypeScript. That said, one could argue that environment variables should know the correct delimiter since they ought to be tailored to the environment.&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;♻️ The env var either:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;🔁 Must be duplicated in every job that needs it, or&lt;/li&gt;
&lt;li&gt;🧪 Set at the workflow level, potentially polluting jobs that don’t use it, or&lt;/li&gt;
&lt;li&gt;🧩 Require a composite action to centralize, which adds complexity. At this point, would it be over-engineered?&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;&lt;p&gt;🔧 Dependency on env var and need for error handling to check that it's set correctly&lt;/p&gt;&lt;/li&gt;

&lt;/ul&gt;

&lt;h2&gt;
  
  
  Another option: Pass in the path as a parameter
&lt;/h2&gt;

&lt;p&gt;While I didn't implement this, I could also have used hardhat tasks instead of scripts.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Pros:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;✅ DRY: within scripts (but not the interface calling the script)&lt;/li&gt;
&lt;li&gt;✅ Scripts are isolated from changes to the file path value&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Cons:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;🚫 Opposes the principle of minimising function arguments&lt;/li&gt;
&lt;li&gt;⚙️ Hardhat tasks require more setup than hardhat scripts&lt;/li&gt;
&lt;li&gt;🔁 The parameters must be passed in every time the hardhat task or bash script is called&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Using a shared Typescript utils file is insufficient. It only solves the problem for a subset of your codebase (TypeScript) and introduces inconsistency. Passing in the path as a parameter keeps code &lt;em&gt;within&lt;/em&gt; scripts DRY, but requires adding an argument to the interface of every script that needs the path.&lt;/p&gt;

&lt;p&gt;The solution I chose was to &lt;strong&gt;centralise the file path as a single env var&lt;/strong&gt;. This provides the cleanest interface for all scripts — especially when dealing with both TypeScript and bash. It decouples the file path from script internals, which is valuable when the directory structure or file name may change.&lt;/p&gt;

&lt;p&gt;Have a different strategy to manage shared variables across different languages? Share your knowledge or suggest improvements in the comments.&lt;/p&gt;

</description>
      <category>typescript</category>
      <category>bash</category>
      <category>learning</category>
      <category>design</category>
    </item>
  </channel>
</rss>
