This is a comprehensive, short guide to just get you started with writing basic bash scripts in Linux. This blog will equip you with the basic and required knowledge on working with bash scripts, responsbility is upon you to learn more and make the best of it 🙂
Prerequisities : Knowledge of basic Linux CLI commands.
“Bash Scripting” – let’s break it down to what it is. First up – “Bash”.
Bash is basically a shell – a place where you can 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 it , much like a “program” .
A ‘Bash Script’ is basically 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. Every bash script must start like this :
This line tells the shell that the script is in the Bash shell (in cases when one has shells like zsh installed, this line becomes crucial)
Moving back to the script, let’s do a simple input – output:
#!/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 :
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
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 [ condition ]
#that ¯ \_(ツ)_/ ¯
That’s as simple as it can get.
if this then that. else if, then do that. fi
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 (Plus the extesnion isn’t really necessary, it’s just a convention of naming)
#!/bin/bash cd # so that we get 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 –
And voila. It works 🙂 (In case it doesnt, open a new terminal and try again)
Bash Scripting is a vast, vast field and what I have covered here so far is barely the tip of the iceberg. If you found this interesting, please do comment 🙂