1753529 Members
4800 Online
108795 Solutions
New Discussion юеВ

Echo and backslash

 
SOLVED
Go to solution
Leo The Cat
Regular Advisor

Echo and backslash

Hi

I'd like to produce the echo below:

123\\abc

I've tried echo "123\\abc" or "123\\\\abc"without succes !

Any Idea ?

Regards
Den
7 REPLIES 7
James R. Ferguson
Acclaimed Contributor
Solution

Re: Echo and backslash

Hi Den:

echo "123\\\\\\\\abc"
123\\abc

Regards!

...JRF...
Steven Schweda
Honored Contributor

Re: Echo and backslash

dy # echo '123\\abc'
123\\abc

dy # echo "123\\\\abc"
123\\abc

dy # uname -a
HP-UX dy B.11.11 U 9000/785 2012616114 unlimited-user license

Of course, that's using bash. With /bin/sh,
it seems to be harder.

dy # echo "123"'\\\\'"abc"
123\\abc
OldSchool
Honored Contributor

Re: Echo and backslash

in "sh"

echo "123\\\\\\acb"

works as well.
James R. Ferguson
Acclaimed Contributor

Re: Echo and backslash

Hi (again) Den:

> Steven: Of course, that's using bash. With /bin/sh, it seems to be harder.

...and without rancor, I would have to say that since this is HP-UX it's fair to assume that we should be using the *standard* HP-UX shell ('/usr/bin/sh' or '/sbin/sh'). The bash shell's handling of backslashes in its implementation of 'echo' differs significantly.

On an 11.11 machine:

# echo ${SHELL}
/sbin/sh
# echo "123\\abc"
123bc
# echo "123\\\\abc"
123\abc
# echo "123\\\\\\\\abc"
123\\abc

Regards!

...JRF...

Leo The Cat
Regular Advisor

Re: Echo and backslash

Thanks !
Steven Schweda
Honored Contributor

Re: Echo and backslash

> ...and without rancor, I would have to say
> that since this is HP-UX it's fair to
> assume that we should be using the
> *standard* HP-UX shell ('/usr/bin/sh' or
> '/sbin/sh').

Assume what you wish, but "should" is your
opinion. Bash runs on HP-UX, too, however.

> The bash shell's handling of
> backslashes in its implementation of 'echo'
> differs significantly.

No argument there.

> # echo ${SHELL}
> /sbin/sh

Not a good test.

dy # echo "123\\\\abc"
123\\abc
dy # echo $SHELL
/sbin/sh

SHELL is not guaranteed to agree with the
current shell.
Dennis Handly
Acclaimed Contributor

Re: Echo and backslash

Basically should should treat backslash as a character only to used to quote other chars. :-)

One thing about quoted strings, you'll need to double up backslashes in "" vs ''.
So one would think that this would work:
echo "123\\\\abc"
echo '123\\abc'

Unfortunately this doesn't work because echo then has its own rules about backslashes.