I'm trying to write a bash script to read a file as input and append the values/data in that file into variables and the values of the variables should be updated each iteration based on the input line from the file.

Eg: Input file looks like below:

100  Thomas  Manager    Sales       $5,000
200  Jason   Developer  Technology  $5,500
300  Sanjay  Sysadmin   Technology  $7,000
400  Nisha   Manager    Marketing   $9,500
500  Randy   DBA        Technology  $6,000

While reading this through my shell script, I'll define 5 variables let's a,b,c,d,e.

First iteration:

The variable values should be assigned with first line of input file.


Second iteration:

The variable values should be assigned with second line of input file.


And so on...

Please anyone give some input on how to write a script to read values in this format.

  • yeah @pa4080, I gone through it. Basically the first one suits pretty much to my scenario to make further modification with the script. As you mentioned in first answer I was missing -a with read command. Great! Thanks for the help
    – Harsha
    Dec 5, 2018 at 10:10
  • 1
    Upvoted but forgot to tick
    – Harsha
    Dec 5, 2018 at 10:14

1 Answer 1


You can use the read builtin command with option -a with a loop to read each line of the file as an array. Then you can assign the value of the array elements to the variables you need (or you can use the array elements directly in your script):



while read -ra LINE
        a="${LINE[0]}"; b="${LINE[1]}"; c="${LINE[2]}"
        d="${LINE[3]}"; e="${LINE[4]}"

        echo -e "$a\n$b\n$c\n$d\n$e\n"

done < "$IN_FILE"

Another way is to assign the values of each column of certain line directly to the variables. The following example uses bash function that will feed the variables with new values when it is called. The function has one input parameter, that determines which line from the file to be parsed.



args_feed() {
        read -r a b c d e <<< "$(sed "$1 q;d" "$IN_FILE")"

# -----------------
# Examples of usage
# -----------------

# Parse the third line
args_feed 3
echo -e "$a\n$b\n$c\n$d\n$e\n"

# Sequential call - First iteration
args_feed $((++LINE_NR))
echo -e "$a\n$b\n$c\n$d\n$e\n"

# Sequential call - Second iteration
args_feed $((++LINE_NR))
echo -e "$a\n$b\n$c\n$d\n$e\n"

# Parse the entire file
NR_LINES="$(cat "$IN_FILE" | wc -l)"
for LINE_NR in $(seq 1 $NR_LINES)
        args_feed "$LINE_NR"
        echo -e "$a\n$b\n$c\n$d\n$e\n"


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.