<?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>Forem: Celestine Ekoh-Ordan</title>
    <description>The latest articles on Forem by Celestine Ekoh-Ordan (@ceoehis).</description>
    <link>https://forem.com/ceoehis</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%2F41248%2F9e1674ed-aeaf-4203-97b0-7fcb575b6b91.png</url>
      <title>Forem: Celestine Ekoh-Ordan</title>
      <link>https://forem.com/ceoehis</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/ceoehis"/>
    <language>en</language>
    <item>
      <title>Getting Started with Bash Scripting for Web Developers</title>
      <dc:creator>Celestine Ekoh-Ordan</dc:creator>
      <pubDate>Tue, 22 Jan 2019 22:40:32 +0000</pubDate>
      <link>https://forem.com/ceoehis/getting-started-with-bash-scripting-for-web-developers-268m</link>
      <guid>https://forem.com/ceoehis/getting-started-with-bash-scripting-for-web-developers-268m</guid>
      <description>&lt;p&gt;Web developers are constantly seeking ways to increase their efficiency, and one key way to do that is to automate, at least to an extent, most mundane and repetitive tasks that constitute our daily activities. One tool that can help with that and requires no overhead to begin to use and leverage upon is Bash Scripting.&lt;/p&gt;

&lt;p&gt;This article aims to get you started with Bash. My aim is to highlight its potential to be an addition to your workflow as a developer.&lt;/p&gt;

&lt;p&gt;As might be obvious from the preceding statements, I'll assume that you have a bit of familiarity with software development, you know what Git and Github are and at least know what a terminal is.&lt;/p&gt;

&lt;p&gt;Having said that what you will need in order to follow along with this tutorial includes:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A terminal (Linux or Mac)&lt;/li&gt;
&lt;li&gt;A text editor&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If you have these, we can begin.&lt;/p&gt;

&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;h3&gt;
  
  
  What is Bash and Bash Scripting
&lt;/h3&gt;

&lt;p&gt;Bash stands for Bourne Again Shell and it is a command processor or interpreter in which a user can type in executable commands. A Bash script, on the other hand, is, simply put, a collection of Bash commands.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--kMUo8brC--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/ucqm6qmeidhtb3ljrqap.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--kMUo8brC--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/ucqm6qmeidhtb3ljrqap.png" alt="terminal"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  A simple hello world script
&lt;/h3&gt;

&lt;p&gt;Let us begin with a simple hello world script.&lt;br&gt;
In Bash, the construct for printing out something to the terminal(console) is the &lt;code&gt;echo&lt;/code&gt; keyword. It is the equivalent of &lt;code&gt;console.log&lt;/code&gt; in JavaScript which simply prints out the arguments supplied to it.&lt;/p&gt;

&lt;p&gt;In order to make our first script we need to create a simple file called &lt;code&gt;hello_world.sh&lt;/code&gt;.&lt;/p&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;&lt;span class="nb"&gt;touch &lt;/span&gt;hello_world.sh
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;To begin with our hello world script we would create a Bash command in our script which we can run and execute. Open the &lt;code&gt;hello_world.sh&lt;/code&gt; file in your text editor and enter the following&lt;/p&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;  &lt;span class="c"&gt;#!/bin/bash&lt;/span&gt;

  &lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"Hello world"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h3&gt;
  
  
  Making an executable
&lt;/h3&gt;

&lt;p&gt;In order to run this script, we would need to make it an executable file. If you list out the files in the current directory using &lt;code&gt;ls -al&lt;/code&gt;, the &lt;code&gt;hello_world.sh&lt;/code&gt; file we created would be listed with the following properties similar to what is shown below&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--jrIrHEb5--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/jac4js3e9gjgcuw765ld.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--jrIrHEb5--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/jac4js3e9gjgcuw765ld.png" alt="list-files"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;-rw-r--r--&lt;/code&gt; section shows that the current user only has &lt;code&gt;r&lt;/code&gt;ead and &lt;code&gt;w&lt;/code&gt;rite permissions on that file.&lt;/p&gt;

&lt;p&gt;In order to execute this script run the following command to add execute permissions for the current user.&lt;/p&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;&lt;span class="nb"&gt;chmod &lt;/span&gt;755 hello_world.sh
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Run &lt;code&gt;ls -al&lt;/code&gt; once more to see the current permissions and you should see something similar to this. The &lt;code&gt;x&lt;/code&gt; showing that the current user has execute permissions for this file.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--dXPPRyx8--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/0a7bileat5amyhunenvu.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--dXPPRyx8--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/0a7bileat5amyhunenvu.png" alt="list-executable"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Running the script
&lt;/h3&gt;

&lt;p&gt;In order to execute our Hello world script just run &lt;code&gt;./hello_world.sh&lt;/code&gt; in the current directory. This Bash script simply prints out the string "Hello world" to the terminal when it is executed.&lt;/p&gt;

&lt;p&gt;Exciting right? 🚀🔥&lt;/p&gt;

&lt;p&gt;What if we could make this function take in an argument and print out that argument instead. And if no argument is provided it can just print out a default "Hello world" greeting.&lt;/p&gt;

&lt;p&gt;We will update the script to handle receiving and use of arguments.&lt;/p&gt;

&lt;h3&gt;
  
  
  Bash Scripting Basics
&lt;/h3&gt;

&lt;h4&gt;
  
  
  Command line parameters
&lt;/h4&gt;

&lt;p&gt;We can pass arguments to our script by supplying them in front of the call to our script. So let's say we wanted to execute our script and pass an argument (e.g a name) to it, we can do it as follows&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s---NodtXIk--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/jjxjw8atrmjip6xfpty1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s---NodtXIk--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/jjxjw8atrmjip6xfpty1.png" alt="hello-param"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Executing our script this way passes the string of characters "Jane" to our script. However, we currently have no way of using this parameter in our current script.&lt;/p&gt;

&lt;p&gt;Command line arguments are usually supplied to the running script as a list of arguments represented by &lt;code&gt;$1&lt;/code&gt; for the first argument, &lt;code&gt;$2&lt;/code&gt; for the second and so on.&lt;/p&gt;

&lt;p&gt;So in our case above, we can update our script to accept a name and print out a "Hello" greeting along with the name. Edit your &lt;code&gt;hello_world.sh&lt;/code&gt; file and replace it with the following:&lt;/p&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;  &lt;span class="c"&gt;#!/bin/bash&lt;/span&gt;

  &lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"Hello"&lt;/span&gt; &lt;span class="nv"&gt;$1&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Executing &lt;code&gt;./hello_world.sh Jane&lt;/code&gt; should print out the expected result&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--n84xxsT3--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/gjs7zh8s7xhx4fmw1wt5.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--n84xxsT3--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/gjs7zh8s7xhx4fmw1wt5.png" alt="hello-jane"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  if...else conditionals
&lt;/h4&gt;

&lt;p&gt;Now we have our script printing the greeting with a provided name. If you run the script without any arguments you will notice it just prints out "Hello". We can extend its functionality by making use of conditionals within Bash. A simple if...else statement would do.&lt;/p&gt;

&lt;p&gt;Update your script to the following:&lt;/p&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;  &lt;span class="c"&gt;#!/bin/bash&lt;/span&gt;

  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;[&lt;/span&gt; &lt;span class="nv"&gt;$1&lt;/span&gt; &lt;span class="o"&gt;]&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="k"&gt;then
    &lt;/span&gt;&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"Hello"&lt;/span&gt; &lt;span class="nv"&gt;$1&lt;/span&gt;
  &lt;span class="k"&gt;else
    &lt;/span&gt;&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"Hello World"&lt;/span&gt;
  &lt;span class="k"&gt;fi&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;With the above, we are checking if a command line argument was supplied to the script and then we print the greeting with the argument, otherwise we print "Hello World". Run it a few times to verify this.&lt;/p&gt;

&lt;h3&gt;
  
  
  A more practical Bash script
&lt;/h3&gt;

&lt;p&gt;Now I understand you can't do much with just a "Hello World" script, so we will try to build a script you can actually use.&lt;/p&gt;

&lt;p&gt;For this, we will look to solve a problem that I think developers have in their daily activity, which is cloning a Github repository and changing directory into the newly cloned repository in one single step.&lt;/p&gt;

&lt;p&gt;Create a new file in a folder of your choice called &lt;code&gt;clone_repo.sh&lt;/code&gt;. Add execute permissions to the file by running &lt;code&gt;chmod 755 clone_repo.sh&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Enter the following into it&lt;/p&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;  &lt;span class="c"&gt;#!/bin/bash&lt;/span&gt;

  &lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"Initializing git clone &lt;/span&gt;&lt;span class="nv"&gt;$1&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;

  git clone &lt;span class="nv"&gt;$1&lt;/span&gt;

  &lt;span class="nb"&gt;basename&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="k"&gt;$(&lt;/span&gt;&lt;span class="nb"&gt;basename&lt;/span&gt; &lt;span class="nv"&gt;$1&lt;/span&gt;&lt;span class="k"&gt;)&lt;/span&gt;
  &lt;span class="nv"&gt;folder_name&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="k"&gt;${&lt;/span&gt;&lt;span class="nv"&gt;basename&lt;/span&gt;&lt;span class="p"&gt;%.*&lt;/span&gt;&lt;span class="k"&gt;}&lt;/span&gt;

  &lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"Changed directory to &lt;/span&gt;&lt;span class="nv"&gt;$folder_name&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;
  &lt;span class="nb"&gt;cd&lt;/span&gt; &lt;span class="nv"&gt;$folder_name&lt;/span&gt;
  &lt;span class="nb"&gt;exec &lt;/span&gt;bash
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;What the above script does is fairly obvious. Firstly we print out a message saying notifying the beginning of the git clone. Then we run the actual git clone command. (Note this script assumes you already have git installed in your environment).&lt;/p&gt;

&lt;p&gt;Next, we make use of &lt;a href="https://en.wikipedia.org/wiki/Basename"&gt;basename&lt;/a&gt; which is a standard Unix program to get the last name from the provided Github URL ignoring any trailing slashes. The next line only sanitizes it in order to get the correct pathname in cases where the Github URL also has a &lt;code&gt;**.git&lt;/code&gt; extension at the end e.g &lt;code&gt;https://github.com/ceoehis/reactive-weather.git&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;After obtaining the correct folder name we notify the user with where we are with the set up via another &lt;code&gt;echo&lt;/code&gt; statement and then issue a command to &lt;code&gt;cd&lt;/code&gt; or change directory into the folder name.&lt;/p&gt;

&lt;p&gt;The final line is a call to execute the current Bash instance at the end of our script in order for the &lt;code&gt;cd&lt;/code&gt; command to take effect on the parent shell. This is because Bash scripts are usually run in a subshell and cannot change the working directory of the parent shell. This means that the effect of the &lt;code&gt;cd&lt;/code&gt; command would ordinarily not affect the parent shell. In order to have the &lt;code&gt;cd&lt;/code&gt; command take effect on the parent shell, we need to &lt;code&gt;exec&lt;/code&gt;ute the child Bash instance in the script as above.&lt;/p&gt;

&lt;p&gt;Finally, we need to execute this script and pass a Github repository as an argument.&lt;/p&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;./clone_repo.sh https://github.com/ceoehis/reactive-weather.git
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--nMdiT0Uc--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/spqr2wb7572j684mvyr1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--nMdiT0Uc--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/spqr2wb7572j684mvyr1.png" alt="clone-repo"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Voila! The repo was cloned successfully and the resulting directory became the current working directory, thanks to our script. 🔥💪🏽&lt;/p&gt;

&lt;p&gt;Our demo script can be improved upon in a few ways. See if you can improve the user experience for this script by handling potential errors, and maybe even prompting a user with the correct usage information for cases when they don't supply a Github URL.&lt;/p&gt;

&lt;p&gt;The possibilities with shell scripting are endless, from printing names to the console to automating several tasks. This tutorial is a primer to get you started with basic Bash constructs and scripts. You can find more resources in the links below.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.shellscript.sh/"&gt;Shell Scripting tutorial&lt;/a&gt;&lt;br&gt;
&lt;a href="https://linuxconfig.org/bash-scripting-tutorial-for-beginners"&gt;Bash for beginners&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Thanks for reading. This article was originally posted on my &lt;a href="https://blog.ekohordan.com/posts/intro-to-bash/"&gt;blog&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>automation</category>
      <category>shell</category>
      <category>productivity</category>
    </item>
  </channel>
</rss>
