最近想生成一个全ff的文件,需要将/dev/zero的0转换成f,输出到文件。
$ dd if=/dev/zero bs=4 count=1 | tr '\0' $'\xff' | hexdump
0000000 ffff ffff
0000004
$ dd if=/dev/zero bs=4 count=1 | tr '\0' '\377' | hexdump
0000000 ffff ffff
0000004
有两种方法,一个是用bash shell的0xf,根据bash的手册。
3.1.2.4 ANSI-C Quoting
Character sequences of the form $’string’ are treated as a special kind of single quotes.
\xHH the eight-bit character whose value is the hexadecimal value HH (one or two
hex digits)
第二种,是tr自己支持转义:
\NNN character with octal value NNN (1 to 3 octal digits)
为了将原本的\NNN喂给tr,所以要加单引号'',避免bash给转义了。