|
Home > Archive > Unix Programming > July 2006 > Escape \ within $variable
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Escape \ within $variable
|
|
| John Smith 2006-07-26, 7:28 pm |
| I am using bash 2.03 on solaris
bash --version
GNU bash, version 2.03.0(1)-release (sparc-sun-solaris)
Copyright 1998 Free Software Foundation, Inc.
I have the following contents in a temp.txt file:
dummy\test
dummytest2
My script needs to read in temp.txt and outputs its contents into
another file temp2.txt as follows:
while read line
do
echo "$line"
done < "$FILENAME"
but if i do this, then I get the following in temp2.txt:
dummytest
dummytest2
How do I preserve the "\" in temp2.txt?
using '$line' would give:
$line
$line
P.S. I already RTFM as follows:
http://www.tldp.org/LDP/abs/html/escapingsection.html
| |
| Chris F.A. Johnson 2006-07-26, 7:28 pm |
| On 2006-07-26, John Smith wrote:
> I am using bash 2.03 on solaris
>
> bash --version
> GNU bash, version 2.03.0(1)-release (sparc-sun-solaris)
> Copyright 1998 Free Software Foundation, Inc.
>
> I have the following contents in a temp.txt file:
> dummy\test
> dummytest2
>
> My script needs to read in temp.txt and outputs its contents into
> another file temp2.txt as follows:
>
> while read line
> do
> echo "$line"
> done < "$FILENAME"
>
> but if i do this, then I get the following in temp2.txt:
> dummytest
> dummytest2
>
> How do I preserve the "\" in temp2.txt?
Use the -r option to read (IFS= preserves leading and trailing
spaces):
while IFS= read -r line
do
printf "%s\n" "$line"
done < "$FILENAME"
> using '$line' would give:
> $line
> $line
>
> P.S. I already RTFM as follows:
> http://www.tldp.org/LDP/abs/html/escapingsection.html
--
Chris F.A. Johnson, author <http://cfaj.freeshell.org>
Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
===== My code in this post, if any, assumes the POSIX locale
===== and is released under the GNU General Public Licence
|
|
|
|
|