Cat Command on File not printing "Blank" Lines?

Hello All,

I have a bash script and in it at some point I call an Expect Script that does some stuff and saves its
output in a ".txt" file.

Example "/path/to/my/file/Expect_Output.txt" file: notice the 2nd line is empty in the file...

Data for Host-1 (192.168.1.110)

Checking the status...
Status is GOOD

Then later on in my Bash script, I run the following command to put all the contents of the file
into an array, line-by-line...

EXPECT_OUTPUT="/path/to/my/file/Expect_Output.txt"

outputArray=( $(cat "$EXPECT_OUTPUT") )

I thought the contents of the array should be this:
(0) = "Data for Host-1 (192.168.1.110)"
(1) = ""
(2) = "Checking the status..."
(3) = "Status is GOOD"

But when I loop through the array and echo out the data I get the following:

for ((x=0; x <= ${#outputArray[@]}; x++))
 do
    echo "${outputArray[$x]}"
done




_____OUTPUT_____
Data for Host-1 (192.168.1.110)
Checking the status...
Status is GOOD

Does anyone know why it wouldn't retain the empty line when saving into the array?
Any thoughts would be much appreciated...!

Thanks in Advance,
Matt

Where the shell splits in a string is controlled by the IFS variable.

By default, it splits on any whitespace.

Do IFS=$'\n' to make it split on newlines.

Really though, do you actually need to store this file in an array? There's probably better ways to do what you want.

1 Like

Hey Corona, thanks for the reply!
And thanks AGAIN for answering another one of my questions so quickly...

Duhh... I completely forgot about the IFS.
That should fix it, thanks!

Thanks Again,
Matt