如何使用echo编写非ASCII字符?

时间:2022-10-20 20:20:47

How do I write non-ASCII characters using echo? Is there an escape sequence, such as \012 or something like that?

如何使用echo编写非ASCII字符?是否有转义序列,例如\ 012或类似的东西?

I want to append ASCII characters to a file using:

我想使用以下命令将ASCII字符附加到文件:

echo ?? >> file

4 个解决方案

#1


23  

Use

使用

echo -e "\012"

#2


29  

If you care about portability, you'll drop echo and use printf(1):

如果你关心可移植性,你将丢弃echo并使用printf(1):

printf '\012'

#3


12  

On my terminal,

在我的终端上,

printf '\012' >>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 '\012' >>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中删除默认的额外换行符。

#4


1  

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"
►

#1


23  

Use

使用

echo -e "\012"

#2


29  

If you care about portability, you'll drop echo and use printf(1):

如果你关心可移植性,你将丢弃echo并使用printf(1):

printf '\012'

#3


12  

On my terminal,

在我的终端上,

printf '\012' >>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 '\012' >>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中删除默认的额外换行符。

#4


1  

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"
►