简体   繁体   English

bash printf反斜杠,然后换行

[英]bash printf backslash then new line

I am trying to create this from bash into ac header 我正在尝试将其从bash转换为ac header

#define XXXXX \

 "id title\n" \ 

 "1  developer\n" \

the script is 

FORMAT="  \"%-4s  %-32s\\\n"

printf "$FORMAT" "id" "title\\n\"" >> $FILE

printf "$FORMAT" "1" "Developer\\n\"" >> $FILE

the result would be 结果将是

"id    title\n"                        \n  "1     Developer\n"                              \n

when I change FORMAT="%-4s %-32s \\\\ \\n" 当我更改FORMAT="%-4s %-32s \\\\ \\n"

I get 我懂了

"id    title\n"                           \ 
"1     Developer\n"                       \ 

and gcc start to complain the extra space after \\ 和海湾合作委员会开始抱怨多余的空间后, \\

It seems that the \\\\ would be interpreted more than once if there is no space. 如果没有空格,似乎\\\\将被解释多次。

without using FORMAT="%-4s %-32s \\\\" 而不使用FORMAT="%-4s %-32s \\\\"

printf "$FORMAT" "id" "title\\n\"" >> $FILE

printf "\n" >> $FILE
...

Is there any better way to handle this? 有没有更好的方法来解决这个问题?

使用十六进制转义序列:

FORMAT="%-4s %-32s \x5C\n"

shell treats double quote and single quote differently. shell对双引号和单引号的处理不同。

Don's use double quote here: 不要在这里使用双引号:

FORMAT="%-4s %-32s \\n"

Use single quote like this and avoid escaping: 这样使用单引号,并避免转义:

FORMAT='%-4s %-32s \n'

OR for printing literal backslash and newline: 或用于打印文字反斜杠和换行符:

FORMAT='%-4s %-32s \\\n'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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