1752758 Members
4762 Online
108789 Solutions
New Discussion юеВ

Re: echo question

 
SOLVED
Go to solution
Anthony khan
Frequent Advisor

echo question

Hi Guys,

I am using echo "\a\nancy\file" but the echo output I get is following..

echo "\a\nancy\file"

ancy
ile

Because \ is a speacial charactor for new line, any idea how to solve this, I want the output of echo would be same as \a\nancy\file.


Thanks in advance

Anthony

10 REPLIES 10
Ian Dennison_1
Honored Contributor

Re: echo question

\n is a special code for the printf command, not the echo command. That's why it does not work.

man printf

Share and Enjoy! Ian
Building a dumber user
James R. Ferguson
Acclaimed Contributor

Re: echo question

Hi Anthony:

# echo '\\a\\nancy\\file'

Regards!

...JRF...
John Meissner
Esteemed Contributor

Re: echo question

try

echo '\\a\\nancy\\file'

works for me. you need to excape the \a the \n and the \f
the escape character is \
All paths lead to destiny
Darren Prior
Honored Contributor

Re: echo question

Hi,

You'll need to escape the backslashes with a backslash, ie:

echo "\\a\\nancy\\file"

You may want to use something like sed to automate this if you've got a lot of these to change ;)

regards,

Darren.
Calm down. It's only ones and zeros...
curt larson_1
Honored Contributor

Re: echo question

looks like my first reply never made it, so I'll try again

you really should be using the "print" command as echo is obsolete. echo and print formats several escape conventions including:
\a bell character
\n newline
\f formfeed
and
\\ backslash

with the echo command your stuck with using \\ for a backslash. with the print command you can also use the -R and -r switches which do not use the \ conventions.

and if your shell has a printf command you can also use or not use the \ conventions depending on your conversion specification (%s or %b).
James R. Ferguson
Acclaimed Contributor

Re: echo question

Hi (again) Anthony:

Curt's suggestion to use the 'print -R' syntax is the most direct answer to your problem. This is documented in the man pages for 'sh-posix'.

Regards!

...JRF...

Anthony khan
Frequent Advisor

Re: echo question

Its working now only thing is left is using sed to change \a\nancy\file to \\a\\nancy\file, can anyone help...


Thanks
Michael Kelly_5
Valued Contributor
Solution

Re: echo question

cat mjk-test
\a\nancy\file

cat mjk-test | sed -e 's/\\/\\\\/g'
\\a\\nancy\\file

HTH,
Michael.
The nice thing about computers is that they do exactly what you tell them. The problem with computers is that they do EXACTLY what you tell them.
Michael Kelly_5
Valued Contributor

Re: echo question

or even this if you really want only one \ before the f.

cat mjk-test | sed -e 's/\\a/\\\\a/' -e 's/\\n/\\\\n/'

HTH,
Michael.
The nice thing about computers is that they do exactly what you tell them. The problem with computers is that they do EXACTLY what you tell them.