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

The IFS (Internal Field Separator) is a special shell variable used for splitting words and line based on its value. The default value is .

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.


  // 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 “” using following code.


    echo "Hello World!";

# Call funHello from anywhere in the script like below


Le’t execute script

$ ./

Hello World!

#2. Bash – Function with Argument

The passing argument to functions is similar to pass an argument to command from shell. Functions receives arguments to $1,$2… etc. Create a shell script using following code.


   echo "First Argument: " $1
   echo "Second Argument: " $2
   echo "Third Argument: " $3
   echo "Fourth Argument: " $4

# Call funArguments from anywhere in the script using parameters like below

funArguments 1 Welcome to TecAdmin 

Let’s execute the script with the bash shell.

$ ./

First Argument : 1
Second Argument : Welcome
Third Argument : to
Fourth Argument : TecAd,om

For more detailed uses of function in bash scripts visit here.

Bash – For Loop

For Loop in Bash

As any other programming language, shell scripts also support for loops to do the repetitive tasks. Read this tutorial to understand uses of for loop with examples.


  // scope of for loop

for loop executes for each parameter once defined. The VARIABLE is initialized with the parameter’s value which can be accessed in inside the for loop scope. These parameters can be any number, string etc.

#1. Bash – For Loop Example

Check below basic for loop which iterates 5 times.

You can also define a range with for loop in the bash script with numeric values.

The arguments can be also a string like:

#2. Bash – For Loop in C Style

You can also write for loop in bash script similar to for loop in c programming. For example to print 1 to 10 numbers.

#3. Bash – For Loop with Files

You can access filenames one by one in for loop under the specified directory. For example, read all files from the current directory.

The above loop will iterate the number of times as the number of files available. It will select one by file in each iteration.

Bash Tutorial

Shell Scripting

The Bash (Born Again) is a Unix shell and command line language written by the Brian Fox. The Born Again shell is used by most of the Unix/Linux operating systems as there default shell.

Bash Scripting Tutorial

This Shell Script tutorial is written for the peoples want to learn the basics of shell script programming (Shell scripting). In this series of tutorials, you will learn about bash scripting, which is very useful for automating the daily tasks, larger tasks easier. It helps to automation of tasks like backups, disk cleanup etc.