echo 命令变体

echo 命令变体

工作了好几年,但现在至少 \begin 和 \end 得到了特别处理。echo 行为的变化肯定是新的。即使 echo 不可移植,并且 dash echo 不支持 -e / -E 选项,echo 仍然应该可以持续工作。

#!/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

在我的计算机上输出

$ 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}

或 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}.

答案1

在 UNIX 兼容中echo\b扩展为退格字符。

sh -c 'echo "a\bc"'

或者

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

将输出

c

其他特殊字符包括(来自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)

一些有用的链接:

相关内容