Why do backslashes disappear when passing an echo?

I have code like this that processes a CSV file:

#!/bin/bash
while read line
do
    variable=$(echo $line | awk -F, '{print $2}')
    echo $variable
 done < ./file.csv

If the CSV file contains any \, when I run this command, the output text does not show \.

How can I guarantee that \it is not deleted?

+5
source share
2 answers

The reason for this behavior is that the built-in readuses \as an escape character. The flag -rdisables this behavior.

So this should work:

while read -r line
  variable=$(echo $line | awk -F, '{print $2}')
  echo $variable
done < ./file.csv

You should also place "..."things like $(...)variables around , for example

variable="$(command)"
echo "$variable"
+13
source

The man page bashsays read:

(\)                               .

+1

All Articles