Bash Scripting 101

04 March, 2019



Before we start,
This is a comprehensive, short guide to just get you started with writing basic bash scripts in Linux. This post will only equip you with the basic knowledge you need to work with bash scripts.
Prerequisities : Knowledge of basic Linux CLI commands.


Bash Scripting?

"Bash Scripting" - let's break it down to what it is. First up - "Bash".

Bash is a shell - a place where you communicate with the core (kernel) in forms of commands (like ls, cat, cd). In other words, it's a command line interface (CLI)

Scripting is fundamentally writing a bunch of commands together and then execute them, much like a "program" .

A 'Bash Script' is a bunch of commands put together in one single 'script'.

Bash scripting is a way of communicating effectively with your hardware.

Your First Bash Script!


Now that you know what bash scripting means, let's try making a bash script of your own.
$ sudo nano firstbashing.sh

.sh is the file extension we use for bash scripts. Before we start writing, always remember:

Every bash script starts with a shebang

#! is the shebang and every bash script starts like this:
#!/bin/bash
echo "Feed me a number" 
read num 
echo "Your number was yum : $num"

Now let's try executing the script. First we need it to make the script "executable"

$ sudo chmod +x firstbashing.sh

And now you can simply do :

$ ./firstbashing.sh
output

Voila.

You can make your own scripts according to your wish and need - if, elif, for, while i.e. all the logical and looping constructs are all there :)


Here's a fairly bash script I made using the if construct and random numbers:

random=$((1 + RANDOM%5)) 
if [ 1 -eq $random ] 
then 
	echo "KEEP MOVING"
elif [ 2 -eq $random ] 
then 
	echo "Goodd Job." 
elif [ 3 -eq $random ] 
then 
	echo "THERES NO TOMORROW" 
elif [ 4 -eq $random ] 
then 
	echo "YOU CAN FUCKING DO IT" 
elif [ 5 -eq $random ] 
then 
	echo "DAMN GOOD JOB!" 
fi

Break-down time:

random=$((1 + RANDOM%5)) 

This line takes use of an **environment variable** named RANDOM, which, as you guessed it, stores a random number. The modulo 5 and plus 1 are just to make the output in between the range of 1 and 5 - because that's what I need for my if - elif construct, which is coming next.
if [ 1 -eq $random ]
	then 

if [this condition is true] then do this

if this then that. else if, then do that. fi
As simple as that :)

Now, to the condition :

[ 1 -eq $random ]

-eq is the comparision operator in bash (it is equivalent to "==") so the statement simply boils down to "if 1 equal to the value in random".

( View the full source of the script here)


How bash scripts can make your life easier

Now, say, you have a bunch of commands you use on a regular basis. You could avoid the typing all of it by simply using a bash script with all of the commands in it, and then placing it under ~/bin and then simply typing out the script's name in the command line.

Let's go over an example -

Say, I run these commands on a regular basis :

 $ cd blender-git/blender 
 $ make update 
 $ make

Step 1 : make a script and name it as you like, I'm calling it "buildb"
 $ sudo nano buildb 

There's no need for the .sh extension as we are making an executable (and technically, the extension isn't really necessary, it's just a convention of naming but it's always good to follow the convention)

#!/bin/bash 
cd           # go to the root directory 
cd blender-git/blender 
make update 
make

And then make it executable -
$ sudo chmod +x buildb


Place this script in ~/bin after making the directory.

$ sudo mkdir ~/bin 
$ sudo mv ~/buildb ~/bin/buildb

Now simply do -
$ buildb

And voila. It works :) (In case it doesn't, open a new terminal instance and try again)


Bash Scripting is a vast, vast field and what I have barely scratched the surface here.




Go back to top

© 2021, Akhil