How I start my development setup now, with Bash

6 Min. Read
Jun 27, 2021

I had heard about Bash in Linux, and how we can do a lot of automation using it. I always found it interesting but never got the chance to look into it, until yesterday… I was scrolling through Youtube looking for something to watch, when I saw this video “Write Your Own Bash Scripts for Automation”, it reminded me of the idea of automating starting development setup with just one command. So after watching this video and a few hours later I was able to automate my development setup.

In this blog, I would like to give a short introduction to bash script, alias, and how I automated my development setup.

What is bash

Bash is a type of interpreter that processes shell commands. We write commands like cd or ls in the terminal, these commands are interpreted by bash and respected action is performed.

What is a bash script

A bash script is a text file containing series of commands that can be executed in the terminal. We can put any command that can be executed in the terminal into a Bash script. Bash script files have an extension of .sh, the first line of the bash file contains the location of the bash interpreter. To get the location of the bash interpreter, we run this command $ which bash.

1
2
$ which bash
/usr/bin/bash

/usr/bin/bash is the location of bash interpreter in my computer, now we add this to the top of the bash file, like this #! /usr/bin/bash, and we write commands below it, and we execute bash file using bash keyword in the terminal, like this $ bash bash_intro.sh.

let’s create a simple bash file using nano (nano is a command-line text editor),

1
   $ nano bash_intro.sh 

nano creates the file if not already exists and opens the file in the terminal text editor. We write the following code in the bash file now.

1
2
3
#! /usr/bin/bash #location of bash interpreter
read name #reads user input and store in the variable, name.
echo "Hello world, I am $name"  #echo displays the string, and $name replaces with the value of name variable.

NOTE: ctrl + s to save the file and ctrl + x to exit the file in nano terminal text editor.

when we execute it in the terminal we get

1
2
3
$ bash bash_intro.sh
Sushil
Hello world, I am Sushil

What is alias

Alias is a (usually shorter) name given to another (usually longer) name or commands.
Syntax: $ alias alias_name="longer names or commands"

let’s create an alias to get IP address of our computer.

we normally do ifconfig (it shows us network information of our computer) and look for the IP address. Now, let’s try this.

1
2
$ ifconfig | grep broadcast
inet 192.168.1.66  netmask 255.255.255.0  broadcast 192.168.1.255

grep(global regular expression print) is a command-line utility for searching plain-text data using regular expression. Here grep searched for the line containing broadcast word and we got the line, and the line contains our IP. Now to get just the IP, we use awk, awk is a programming language, we pass the output of the above command to awk, and print the 2nd value of the line.

1
2
$ ifconfig | grep broadcast | awk '{print $2}'
192.168.1.66

Let’s make this an alias now,

1
2
3
$ alias whatismyip="ifconfig | grep broadcast | awk '{print $2}'"
$ whatismyip
192.168.1.66

Awesome!, now we have got a word to get the IP address, but, there is a small problem, defining alias in this way makes it temporary, and sessional, once we close this terminal or try and use the alias in another terminal, it will not be found. To make our alias persistent, we need to define them in a script file called .bashrc. This script file is executed by Operating System when the user logs in.
To open this file, we use nano again.

1
`nano ~/.bashrc`

we can see configurations, PATH definitions, command aliases in this file. We can add our alias in this file,but to make the .bashrc file clean and modular, there is .bash_aliases file, where we can write our aliases and it gets merged back to .bashrc, so we are gonna add our custom aliases in .bash_aliases file. To open this file:

1
`nano ~/.bash_aliases`

Now, we define our alias in this file

1
alias whatismyip="ifconfig | grep broadcast | awk '{print $2}'"

save and exit the file. We need to close the terminal and reopen it to update the terminal to our new alias configurations. Now, the alias is persistent, and we can use whatismyip to get our IP address.

MIDPOINT

We have reached the mid-point of this blog, at this point, I would like to thank you for your patience and reading it up to here. Now, I will show you how I automated development setup in my computer, and hopefully, you can automate in yours as well.

My Operating System: Ubuntu 20.04.2 LTS. This method is supported by many popular Linux distributions, including CentOS, Debian, Red Hat, Ubuntu, etc.We can implement similar solution in windows as well.

How I used to start my Optonome development setup after opening my computer.

1
2
3
4
5
6
: Click on Files from my sidebar
: Double click 'Acer' (my drive containing the development folder), this mounts the drive.
: Open the folder containing the Optonome project.
: Right-click on the folder and click on 'Open in terminal'
: `code .` to open VSCode in that folder
: after the VSCode opens, `yarn start` in the terminal to start my server

How I do it now.

1
2
: open terminal by `Ctrl + Alt + t`  
: `optocode` in the terminal

optocode is my custom alias, it opens vscode at that folder and starts the server.

Here is how I did it

nano ~/.bash_aliases:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#bash function to change my directory to my project directories
gtd() {
    if [ "$1" = "optonome" ] #"$1" replaces to first parameter of gtd(go to directory) function 
    then 
        cd /mnt/sdb3/Ideabreed/optonome_web 
        pwd # displays the directory path
    else
        echo "Invalid directory code"
    fi
}


#alias for opening vscode and starting yarn server
alias codestart="code .; yarn start"


#alias to automate development start
alias optocode="gtd optonome; codestart"

Now, with a single command optocode, I can open vscode at my optonome project folder and start my server. This way has made it much easier and faster to open and start developement project.

One of the problem I had faced when building this process was, Ubuntu doesnot mount drive partition when it starts. So, when I start my computer and type optocode, it would say directory path not found, cause the directory has not been mounted yet. I searched how to mount drives with command and tried few commands, nearly had a panic attack, thinking I had overwritten my drive containing work directories, ( restarting my computer fixed it ).
To solve this drive not auto mounting on start problem, I found a youtube tutorial Easy way to Mount Partition Automatically on Start Up ..., I used it to mount my work drive on start up and that fixed it.

We can even add other project directories and services, I have added ideadbreed project as well, like this: nano ~/.bash_aliases:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#bash function to change my directory to my project directories
gtd() {
    if [ "$1" = "optonome" ] 
    then 
        cd /mnt/sdb3/Ideabreed/optonome_web 
        pwd
    elif [ "$1" = "ideabreed" ] 
    then 
        cd /mnt/sdb3/Ideabreed/ideabreed.net 
        pwd
    else
        echo "Invalid directory code"
    fi
}


#alias for opening vscode and starting yarn server
alias codestart="code .; yarn start"

#alias for opening vscode and starting middleman
alias codeman="code .; middleman s -p 3000"

#alias to automate development start
alias optocode="gtd optonome; codestart"
alias ideaman="gtd ideabreed; codeman"

To implement it in your linux system, first if your project containing disk partition is not mounted automatically at start, you have to change disk settings to mount on start. Watch this video for it: Easy way to Mount Partition Automatically on Start Up ....

NOTE: In the “Edit Mount Options” after selecting Mount at system startup, also select the “Identify As” value to something simpler like “/dev/sdb3”.

Now, replace the path of cd in above code to your project directory cd "your_project_path". And close and open terminal to check it out.