0

I have the following beginning to a bash script;

#!/bin/bash

echo "Starting ..."

while IFS="" read -r day || [ -n "$day" ]; do

    while IFS="" read -r meal || [ -n "$meal" ]; do

        echo "Please select a recipe for $meal on $day"

    done < lists/meals

done < lists/days

However, the output is this;

$ ./create.sh
 on mondayect a recipe for breakfast
 on mondayect a recipe for lunch
 on mondayect a recipe for dinner
Please select a recipe for snack on monday
 on tuesdayct a recipe for breakfast
 on tuesdayct a recipe for lunch
 on tuesdayct a recipe for dinner
Please select a recipe for snack on tuesday
 on wednesday a recipe for breakfast

etc...

I was expecting it to read;

$ ./create.sh
Please select a recipe for breakfast on monday
Please select a recipe for lunch on monday
Please select a recipe for dinner on monday
Please select a recipe for snack on monday

Any idea what is going on here?

Thank you.

  • 2
    Check you input files for windows line endings. If there is a `\r` in there, then the cursor jumps to the start of the line and text will be overwritten. For a quick check, use `< <(tr -d \\r file)` instead of `< file`. – Socowi Dec 12 '20 at 20:51
  • 1
    `while IFS=$'\r' read ...` will also trim carriage returns from the end of lines as they're read. – Gordon Davisson Dec 13 '20 at 03:59
  • I resolved this issue using your answers; as I was writing my bash script in Visual Studio Code on Windows, it was auto generating CRLF line endings in my text files. I have now amended them to be LF instead and all works OK. –  Dec 13 '20 at 11:13

0 Answers0