My Tips and Tricks for Bash Scripting After Writing Hundreds of Scripts

<p>As a DevOps engineer, I&rsquo;ve often encountered with Bash. Doing something on a server, writing CI/CD pipelines, or automating some manual task are all regular activities that regularly include writing some Bash scripts. Here I&rsquo;ll share my tips and best practices to follow when scripting in Bash.</p> <p><img alt="" src="https://miro.medium.com/v2/resize:fit:700/0*FIlY4CNDO7OUgK-x" style="height:525px; width:700px" /></p> <p>Photo by&nbsp;<a href="https://unsplash.com/@6heinz3r?utm_source=medium&amp;utm_medium=referral" rel="noopener ugc nofollow" target="_blank">Gabriel Heinzer</a>&nbsp;on&nbsp;<a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral" rel="noopener ugc nofollow" target="_blank">Unsplash</a></p> <h1>1. Distinguish variables</h1> <p>When it comes to variables in Bash, there are two well-known good practices.</p> <ol> <li>Always put curly brackets&nbsp;<code>{}</code>&nbsp;around the variable name.</li> <li>Always put double quotes&nbsp;<code>&quot;&quot;</code>&nbsp;around the variable.</li> </ol> <p>Therefore, a variable should look like this:&nbsp;<code>&quot;${variable_name}&quot;</code>&nbsp;. This way variables are clearly distinguished from the rest of the code and spaces inside a variable value will not cause problems. Let&rsquo;s illustrate the problem with spaces on an example.</p> <p><a href="https://levelup.gitconnected.com/my-tips-and-tricks-for-bash-scripting-after-writing-hundreds-of-scripts-59987855b20a"><strong>Website</strong></a></p>