0

I'm running a loop on each line in a file.

if [ -e "/tmp/history" ]; then
  while read line; do
    connect.sh $line \
      && break
  done </tmp/history
fi

The file is formatted like:

user\ name user\ password

So that each line will contain $1 and $2 for connect.sh.

Yet it seems with my while loop $line loses the line breaks before passing to connect.sh so that user\ name becomes user name.

Is there a way I can modify my while loop to maintain the line breaks?

2

1 Answer 1

1

I was able to fix the problem by adding the -r flag and double quoting the variable.

if [ -e "/tmp/history" ]; then
  while read -r line; do
    connect.sh "$line" \
      && break
  done <"/tmp/history"
fi

Similar SO question.

1
  • 1
    If you want to preserve leading/trailing/interior whitespace, use while IFS= read -r line -- this is the idiomatic way to read lines of a file absolutely verbatim. Commented Nov 22, 2017 at 16:55

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .