Read file line by line?

Shell Script to Read File Brief: This example will help you to read a file in a bash script. This tutorial contains two methods to read a file line by line using a shell script. Method 1 – Using simple loop You can use while read loop to read a file content line by line and store into a variable.

Note – In above script line is a variable only. You can use any variable name in place of the line of your choice. Method 2 – Using IFS…

Read More

Bash – Case

Switch (Case) Statment in Bash The case statement is useful and processes faster than an else-if ladder. Instead of checking all if-else conditions, the case statement directly select the block to execute based on an input. Create the first program using the case statement in a shell script. The program takes input from the user and executes statements based on the input value.

Save the above script in case1.sh and execute the script on bash shell. chmod +x case1.sh ./case1.sh Enter your choice [yes/no]:yes Thank you Your type: Yes…

Read More

Concatenate Two Strings

Shell Script to Concatenate Two Strings Brief: This example will help you to concatenate two or more strings variable in a bash script. This tutorial helps you with multiple shell script examples of concatenating strings in a shell script. The first example is a general way to concatenate variables of string. You can simply write all the variable one after another:

Output: Welcome TecAdmin! Another Example You can also use += operator to concatenate two strings and store results in the first string.

Output: Welcome TecAdmin! One More…

Read More

Check if two strings are equal?

Bash – Check If Two Strings are Equal Brief: This example will help you to understand to check if two strings are equal in a bash script. This shell script accepts two string in variables and checks if they are identical. Details Use == operator with bash if statement to check if two strings are equal. You can also use != to check if two string are not equal. You must use single space before and after the == and != operators. Example In this script two variables are initialized…

Read More

Add two numbers?

Shell Script to Add Two Integers Brief: This example will help you to understand to add two numbers in the bash script. This script takes the input of two numbers from the user and prints the sum of both numbers. This is an example script initializes two variables with numeric values. Then perform an addition operation on both values and store results in the third variable.

Command Line Arguments In this second example, shell script reads two numbers as command line parameters and perform the addition operation.

Output:…

Read More

Bash – Include Files

Include Files in Bash Similar to other programming languages which allow to include other files to a file, Bash scripting also allows to include (source) another shell script file to script file. For example, to include config.sh script to current script file use following syntax, where config.sh is available in the same directory of the current script.

Include Shell Script in Other Shell Script For this example, First, I have created a data.sh file with the following content.

Now create another file show.sh, which includes data.sh file.

Read More

Bash – Functions

Functions in Bash A function which can also be referred to as subroutine or procedure is a block of code used for specific tasks. The function also has a property called re-usability. Bash script also provides functions. Syntax: funcationName(){ // scope of function } functionName //calling of function #1. Bash – Create Function Example Create your first function in shell script showing output “Hello World!”. Create a shell script “script.sh” using following code. #!/bin/bash funHello(){ echo “Hello World!”; } # Call funHello from anywhere in the script like below funHello…

Read More

Bash – For Loop

For Loop in Bash In a programming language, a loop is used to repeat the execution of a block of code until the satisfied defined condition. Which is helpful to perform repetitive tasks. Mainly there are 3 types of loops, for, do, and do-while. In this tutorial, we will discuss for loop in shell scripting. The shell scripting also provides for loops to perform repetitive tasks. A basic for loop syntax looks like: Syntax: for VARIABLE in PARAM1 PARAM2 PARAM3 do //for-loop statements done The for loop executes for all…

Read More

Bash – String Comparisons

Bash String Comparisons Use double equals ( == ) operator to compare strings inside square brackets []. Using this option you simply test if two given strings are equals or not inside bash shell scripts. Example Syntax: if [ “$str1” == “$str2” ] # True if equal if [ “$str1” != “$str2” ] # True if not equal Example: For example to compare two string are equal or not. if [ “hello” == “hello” ] ## True if [ “hello” == “hello1” ] ## False if [ “hello” != “hello”…

Read More

Bash – Numeric Comparisons

Bash Numeric Comparisons Under bash shell, you can directly compare numeric values using double parentheses like “((expression))”. Syntax: ((n1 > n2)) Example: Compare two values and check if one is greater than other value. Write below script in compare.sh file.

Now execute this script using bash shell $ chmod +x ./compare.sh $ ./compare.sh True Bash – Numeric Comparisons Operators You can try with many more comparison operators in bash shell to compare two numeric values. ((n1 == n2)) ## n1 is equals to n2 ((n1 != n2)) ## n1…

Read More