Simple Script in Linux "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.Loops;There are three types of loops in Bash:
- For Loop
- While Loop
- Until Loop
for i in seq 1 10;
This will print numbers 1 to 10While 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 variableUntil Loop Usage:
until [ $counter -lt 10 ]; do
echo "counter $counter"
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.