Home 0P3N Blog Simple Script in Linux "Bash"
Ready to Start Your Career?
Create Free Account
mrNayaNi s profile image
By: mrNayaNi
June 8, 2016

Simple Script in Linux "Bash"

By: mrNayaNi
June 8, 2016
mrNayaNi s profile image
By: mrNayaNi
June 8, 2016
Simple Script in Linux's "Bash" - CybraryIntroduction: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:

i=10

Remember not to put space between them.

 Classic "if" statement:

if [ 1 -eq 2 ]; then

echo "1 is equals to 2"

else

echo "1 is not equal to 2"

fi

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"

else

echo "Hello is not equal to world"

fi

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.

 Loops;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;

do

echo $i

done

This will print numbers 1 to 10

 While Loop Usage:

number=0

while [ $number -lt 10 ]; do

echo "Number is $number"

$((number++))

done

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:

counter=20

until [ $counter -lt 10 ]; do

echo "counter $counter"

$((counter--))

done

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

 Conclusion:Today, I showed you how to write simple Bash scripts. I'll soon write more. Hope you learned a lot.
Schedule Demo

Build your Cybersecurity or IT Career

Accelerate in your role, earn new certifications, and develop cutting-edge skills using the fastest growing catalog in the industry