Echo in bash script doesn't print \

Hi,

I have written a script to read a text file and substitute the values from the left to values on the right in all the files in some directory. The first “=” is my delimiter and anything can be the value on the right. The script looks like this :

#!/bin/bash

egrep -v '^#' /home/glistwan/test.properties |
while read inputline
do
  left="$(echo $inputline | cut -d= -f1)"
  right="$(echo $inputline | cut -d= -f2-)"
#  echo left = $left and right = $right
  grep -rl $left /home/glistwan/patched-jboss/ | xargs sed -i "s/$left/$right/g"
done

exit 0

The problem is that if I have a line like this in the test.properties file :

A=\kljlkwq\fgew\r

echo doesn’t print “” signs. Why is that ? The default for echo is -E meaning not to interpret “”.
Is bash responsible for this ?
How can I avoid the problem ? I don’t mind using perl, python or awk for this if it helps.

Thanks for help in advance.

Best regards,
Greg

I don’t see the problem (here).


$ line='A=\kljlkwq\fgew\r'
$ echo $line
A=\kljlkwq\fgew\r

Adding -r to read command seems to have done the trick :

#!/bin/sh

egrep -v '^#' /home/glistwan/test.properties |
while read -r inputline
do
  echo $inputline
  left="$(echo $inputline | cut -d= -f1)"
  right="$(echo $inputline | cut -d= -f2-)"
#  echo left = $left and right = $right
  grep -rl $left /home/glistwan/patched-jboss/ | xargs sed -i "s/$left/$right/g"
done

exit 0

Best regards,
Greg

This has nothing to do with *echo. *When you do

A=\kljlkwq\fgew\r

in bash, the first thing the shell does is scanning the line for any escapes. And as you should know, the \ is an escape for the character directly after it. Now you normaly escape characters that have a special meaning to the shell (like a space character) to let it loose it’s special meaning (in the case of the space it will no longer be “white space” between arguments). But, as k does not have any special meaning at all, \k will simply reduce to* k*. Thus after that first scan the line reads

A=kljlkwqfgewr

and that will be the string value put into the variable* A*. That is way before your echo command.