4

Worked for years but now at least \begin and \end are handled especially. The change in echo behaviour must be new. Even if echo is not portable and the dash echo doesnt support -e / -E options nevertheless echo should work consistently.

#!/bin/sh
# Test

# ok:
cat <<EOF  >> $$.tex
\begin{document}
EOF
# bad, worked in earlier releases
echo "\begin{document}" >> $$.tex
# ok:
echo "\\\begin{document}" >> $$.tex
#ok:
echo "\include{xxx}" >> $$.tex
# ok:
echo "\\\end{document}" >> $$.tex
# unexpected bad, worked before 
echo "\end{document}" >> $$.tex

cat $$.tex

Output on my machine with

$ uname -a
Linux courant 4.15.0-33-generic #36-Ubuntu SMP Wed Aug 15 16:00:05 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux</br>

$ test.sh
\begin{document}
egin{document}
\begin{document}
\include{xxx}
\end{document}
d{document}

or xxd:

00000000: 5c62 6567 696e 7b64 6f63 756d 656e 747d  \begin{document}
00000010: 0a08 6567 696e 7b64 6f63 756d 656e 747d  ..egin{document}
00000020: 0a5c 6265 6769 6e7b 646f 6375 6d65 6e74  .\begin{document
00000030: 7d0a 5c69 6e63 6c75 6465 7b78 7878 7d0a  }.\include{xxx}.
00000040: 5c65 6e64 7b64 6f63 756d 656e 747d 0a1b  \end{document}..
00000050: 6e64 7b64 6f63 756d 656e 747d 0a         nd{document}.
  • 2
    Did you try 'single quotes' instead of "double quotes"? Also for exact and consistent output you should rather look at printf than echo. – Byte Commander Sep 2 '18 at 22:35
  • 1
    You may find the discussion here helpful: Why is printf better than echo? – steeldriver Sep 2 '18 at 23:06
  • Maybe you used bash instead of sh before. – pLumo Sep 4 '18 at 7:22
  • Sorry for confusion, but the question was "does anybody know why backslash (\) when used in front of begin needs to be escaped but not in front of include when using echo in recent ubuntu release. – Horst Fiedler Sep 4 '18 at 7:28
  • "UNIX conformant echos expand \b into a backspace character" (via) – pLumo Sep 4 '18 at 7:33
0

In UNIX conformant echo, \b is expanded into a backspace character.

sh -c 'echo "a\bc"'

or

echo -e "a\bc" # (bash)

will output

c

Other special characters are (from man echo)

   \\     backslash

   \a     alert (BEL)

   \b     backspace

   \c     produce no further output

   \e     escape

   \f     form feed

   \n     new line

   \r     carriage return

   \t     horizontal tab

   \v     vertical tab

   \0NNN  byte with octal value NNN (1 to 3 digits)

   \xHH   byte with hexadecimal value HH (1 to 2 digits)

Some useful Links:

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.