Operating System - HP-UX
1826062 Members
3878 Online
109690 Solutions
New Discussion

Make echo ignore the "\" char

 
SOLVED
Go to solution
Mike_316
Frequent Advisor

Make echo ignore the "\" char

Hey!

Does anyone know how to make echo ignore the special meaning of the "\" character.

I.E. I would like to be able to store the line "c:\temp" in a variable (var) and then use the command "echo $var" to get "c:\temp" to print. HOWEVER, what is happening now is I store "c:\temp" in var and the "echo $var" responds with ...

"c: emp"

... or "c COLON TAB emp" because it interprets the "\t" part of "\temp" as the escape code for "tab".

Thanks!

Mike
"If we treated each person we met as if they were carrying an unspeakable burden, we might treat each other as we should" - Dale Carnegie
11 REPLIES 11
Geoff Wild
Honored Contributor

Re: Make echo ignore the "\" char

With unix it's easy - just escape it a couple of times:

# export TEST="c:\\\test"

# echo $TEST
c:\test

Rgds...Geoff

Proverbs 3:5,6 Trust in the Lord with all your heart and lean not on your own understanding; in all your ways acknowledge him, and he will make all your paths straight.
Ken Hubnik_2
Honored Contributor

Re: Make echo ignore the "\" char

kenvar=c:/\temp
export kenvar
Mike_316
Frequent Advisor

Re: Make echo ignore the "\" char

Great answers...but I left one peice of info out...I do not hav control over the format of the "C:\temp" example, as it is being pulled from another file...so I can add additional "\" to it.

Basically, I need to be able to read "C:\temp" into a variable from a file, and echo it (or print it using any other method) exactly as read to another file.

Thanks forthe help!

Mike
"If we treated each person we met as if they were carrying an unspeakable burden, we might treat each other as we should" - Dale Carnegie
Leif Halvarsson_2
Honored Contributor

Re: Make echo ignore the "\" char

Hi,
I don't know if this vcan be something for you:

a='C:\ddd'
# echo $a
C:\ddd
Zafar A. Mohammed_1
Trusted Contributor

Re: Make echo ignore the "\" char

var="c:\\\test"
echo $var


Thanks
Zafar
john korterman
Honored Contributor
Solution

Re: Make echo ignore the "\" char

Hi Mike,
try this korn-shell feature:

# ksh
$
$ MYVAR="C:\flimflam"
$ print -r "$MYVAR"
C:\flimflam

consult man page for ksh regarding print.

regards,
John K.
it would be nice if you always got a second chance
Geoff Wild
Honored Contributor

Re: Make echo ignore the "\" char

Can you post the file? and /or how you get the variable?
Proverbs 3:5,6 Trust in the Lord with all your heart and lean not on your own understanding; in all your ways acknowledge him, and he will make all your paths straight.
Leif Halvarsson_2
Honored Contributor

Re: Make echo ignore the "\" char

Hi
I think the problem is not the echo command, it is the read.
try
# cat zzz
C:\ddd
# while read -r a
> do
> echo $a
> done C:\ddd
Olivier ROBERT
Frequent Advisor

Re: Make echo ignore the "\" char

Hi Mike,

This one is awful, but it works:

awk 'END { print ENVIRON["var"] }'
Replace echo with the above line... Another solution would be to write a small C program which prints all of its nonzero arguments... I didn't find a better solution to this simple problem!

Hehe, by the way, this is a major ITRC issue, look at this one:

1) Click on the name of one of the contributors of this thread (author excluded), to view its profile
2) Have a look at the thread subject, in the "Responses" column
3) Where the heck is the backslash?

Mike, I wonder if you're working on the ITRC web interface!

Regards,

Olivier
Mike_316
Frequent Advisor

Re: Make echo ignore the "\" char

Perfect!! The real answer was a combination of thesuggesstions to use "print -r " AND "read -r."

I was using the "cat filename | while read var" to get the lines from the file. Unfortunately, SOME ofthe lines had double "\" (I.E. C:\\Temp", and "print -r" by itself was still outputting "C:\temp" for these as well.

However changing the script to (paraphrased)...

#!/bin/ksh
cat filename | while read -r line
do
print -r $line >> newfile
done

...managed to allow the line top be read and written without corruption.

Thanks a million!

Mike
"If we treated each person we met as if they were carrying an unspeakable burden, we might treat each other as we should" - Dale Carnegie
Mike_316
Frequent Advisor

Re: Make echo ignore the "\" char

Perfect!! The real answer was a combination of thesuggesstions to use "print -r " AND "read -r."

I was using the "cat filename | while read var" to get the lines from the file. Unfortunately, SOME ofthe lines had double "\" (I.E. C:\\Temp", and "print -r" by itself was still outputting "C:\temp" for these as well.

However changing the script to (paraphrased)...

#!/bin/ksh
cat filename | while read -r line
do
print -r $line >> newfile
done

...managed to allow the line to be read and written without corruption.

Thanks a million!

Mike
"If we treated each person we met as if they were carrying an unspeakable burden, we might treat each other as we should" - Dale Carnegie