简体   繁体   中英

Print “\n” into file using bash

I want to print this string:

.asciz "%d\n"

into a file. When i try now it says:

.asciz "0\n"

it seems like it interpretes the %d as 0.

Now i write echo ".asciz "\\""%d\\\\\\n"\\""

On modern GNU/Linux systems, inside a single-quoted string nothing is interpreted, and you can simply run the following:

echo '.asciz "%d\n"' > file

The single quotes within should be escaped, however:

echo '13'\'''
# outputs 13'

But there are exceptions . For example, if you put echo '1\\n2' into some-script.sh file and call it with modified BASHOPTS environment variable, the command may replace \\n with the newline character:

$ env BASHOPTS=xpg_echo bash some-script.sh
1
2

So, strictly speaking, echo is unsafe. Alternatively, you can invoke printf :

printf '%s' '.asciz "%d\n"'

Or the built-in version:

builtin printf '%s' '.asciz "%d\n"'

(see info bash printf ).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM