Linux 如何使用 echo 编写非 ASCII 字符?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/657846/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I write non-ASCII characters using echo?
提问by flybywire
采纳答案by vartec
Use
用
echo -e "2"
回答by Nietzche-jou
If you care about portability, you'll drop echoand use printf(1):
如果您关心可移植性,您将删除echo并使用printf(1):
printf '2'
回答by mareoraft
On my terminal,
在我的终端上,
printf '2' >>output.txt
works for both the octal representation of the ascii character, and the corresponding hexadecimal:
适用于 ascii 字符的八进制表示和相应的十六进制:
printf '\xA' >>output.txt
The command
命令
echo -en '2' >>output.txt
however, does not function properly. Only hexadecimals seem to work with echo -e. The -n removes the default extra newline from echo.
但是,不能正常工作。似乎只有十六进制可以与 echo -e 一起使用。-n 从 echo 中删除默认的额外换行符。
回答by Pysis
I took non-ASCII to mean Unicode, at least in my case, but printf "\x##"
wasn't enough for my 2-byte solution, so I used this slightly different syntax instead:
我用非 ASCII 来表示 Unicode,至少在我的情况下,但printf "\x##"
对于我的 2 字节解决方案来说还不够,所以我使用了这个稍微不同的语法:
> printf "\u25ba"
?
回答by wisbucky
You can use ANSI-C Quoting with echo
:
您可以使用 ANSI-C 引用echo
:
echo $'2' # octal
echo $'\x0a' # hex