Simple Script in Linux “Bash”

June 8, 2016 | Views: 5496

Begin Learning Cyber Security for FREE Now!

FREE REGISTRATIONAlready a Member Login Here


Bash is the scripting language used in Linux – it stands for Bourne Again Shell.

You can perform several tasks with this scripting language. It can make your lives easy using simple words. There are some tasks you have to do every day; you can write a script to do them for you instead of repeating these tasks and wasting time.


Simple Usage: 

Here’s how you write “Hello World” in Bash:

echo “Hello World”

That’s it. Wait…is there a trap? It can’t be that easy! It really is that easy.


This is how you declare a variable in Bash:


Remember not to put space between them.


Classic “if” statement:

if [ 1 -eq 2 ]; then

echo “1 is equals to 2”


echo “1 is not equal to 2”


This code will check whether 1 is equal to 2 or not.


Let’s compare strings in another example of an “if” statement:

if [ “Hello” == ‘world” ];then;

echo “Hello is equal to world”


echo “Hello is not equal to world”


You may have noticed that when I compared strings, I used double equals to (==). When comparing numbers, I used (-eq). In Bash, when comparing numbers, we’ll use -eq(equals to), -lt (less than), -gt (greater than) etc. and to compare strings we’ll use. e.g. == (equals to), != (not equals to) etc.



There are three types of loops in Bash:

  1. For Loop
  2. While Loop
  3. Until Loop


For Loop Usage:

for i in seq 1 10;


echo $i


This will print numbers 1 to 10


While Loop Usage:


while [ $number -lt 10 ]; do

echo “Number is $number”



This code starts with declaring a variable that’s a number, which is equal to 0. The condition is while number is less than 10 than print Number is $number the variable $((number++)) this line will increment the variable


Until Loop Usage:


until [ $counter -lt 10 ]; do

echo “counter $counter”



This code will decrement the variable counter until it reaches 10.



Today, I showed you how to write simple Bash scripts. I’ll soon write more. Hope you learned a lot.

Share with Friends
Use Cybytes and
Tip the Author!
Share with Friends
Ready to share your knowledge and expertise?
  1. Very easy to understand

Page 3 of 3«123
Comment on This

You must be logged in to post a comment.

Our Revolution

We believe Cyber Security training should be free, for everyone, FOREVER. Everyone, everywhere, deserves the OPPORTUNITY to learn, begin and grow a career in this fascinating field. Therefore, Cybrary is a free community where people, companies and training come together to give everyone the ability to collaborate in an open source way that is revolutionizing the cyber security educational experience.

Support Cybrary

Donate Here to Get This Month's Donor Badge


We recommend always using caution when following any link

Are you sure you want to continue?