🤔 Research suggests that in an eight-hour day, an average developer is only productive for two hours and 53 minutes. Wasting this productive time doing mundane repetitive tasks doesn’t really seem to be an option now. An ideal goal should be to automate anything that can be automated. Save the effort and time for actual meaningful work.
This article will give a very basic introduction, enough for any beginner to write his/her first shell script. Basically, bash scripts bind multiple commands together, so that with one command you can execute a whole workflow. Script files carry the extension .sh.
Writing a Shell Script File
Let’s write a simple script that will print “My first shell script!” on the terminal screen as you run it. Open your code editor and create a file, name it my-first-script.sh. Consider the following lines.
#!/bin/bash is called a shebang. It merely tells the interpreter about the shell your script is going to run on. In this case, it is pointing towards the absolute path of the bash shell. Next line is an echo statement which will print the string. Save the file.
Running the Script
Open the terminal in the same directory where the script file is located. Now, type sh before the filename in the terminal to run your script.
💥 And boom! It prints the string on the screen. As simple as that.
You can also run the script file using bash instead of sh. Another way is to use chmod. You can set an executable permission using the chmod command as follows:
chmod +x my-first-script.sh
So, you can run it by any of the following commands:
sh my-first-script.sh OR bash my-first-script.sh OR by chmod ./my-first-script.sh
With that done, let’s consider an example script that will be actually useful in a real-life scenario.
Automate Git Repository Creation
Let’s say you want to create a new repository on GitHub. Without automation, it involves multiple steps and can take a few minutes. Let’s make it easier. How about a single command that creates a repository of the name you want, declares it private or public, adds a description, clones the repository locally in the current directory, updates README.md and pushes all changes? Sounds good? It’s pretty simple.
Create a file called repo.sh or whatever you want to name it. Let’s take a look at the following code.
$1, $2 and $3 are shell parameters that will be written after the main command. The first one will be the name of the repository, followed by the description and third being the type of repository. We assign these parameters to variables. Next up are the commands which use GitHub CLI.
gh re --new "$NAME" --description "$DSC" --type "$TYPE" gh re --clone --repo "$NAME"
The first line creates a new repository and sets its name, description, and type according to the parameters. The second one clones the repository. You can find all of these in GitHub CLI made with NodeJS.
The rest of the code is pretty self-explanatory. We move to the cloned repository, commit and push the changes and display a success message. Let’s run this script. Write this command in the terminal:
sh repo.sh MyRepo Test Private
There you go! It sets up a private repository named MyRepo. Now, what if you want this command to be accessible globally in your bash shell? To do that, we add it as a function in our .bashrc file. Find your .bashrc file. Now we need to create a function, you’ll be using the name of the function to call this script. I named it ghinit.
Note that we don’t need shebang in .bashrc. Now in any directory, simply type ghinit and parameters to run this script and set up a new repository.
Handy isn’t it? There is a lot more that can be done with scripts, go explore. 😉