My Tips and Tricks for Bash Scripting After Writing Hundreds of Scripts
<p>As a DevOps engineer, I’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’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 <a href="https://unsplash.com/@6heinz3r?utm_source=medium&utm_medium=referral" rel="noopener ugc nofollow" target="_blank">Gabriel Heinzer</a> on <a href="https://unsplash.com/?utm_source=medium&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 <code>{}</code> around the variable name.</li>
<li>Always put double quotes <code>""</code> around the variable.</li>
</ol>
<p>Therefore, a variable should look like this: <code>"${variable_name}"</code> . This way variables are clearly distinguished from the rest of the code and spaces inside a variable value will not cause problems. Let’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>