簡體   English   中英

bash printf反斜杠,然后換行

[英]bash printf backslash then new line

我正在嘗試將其從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

結果將是

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

當我更改FORMAT="%-4s %-32s \\\\ \\n"

我懂了

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

和海灣合作委員會開始抱怨多余的空間后, \\

如果沒有空格,似乎\\\\將被解釋多次。

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

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

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

有沒有更好的方法來解決這個問題?

使用十六進制轉義序列:

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

shell對雙引號和單引號的處理不同。

不要在這里使用雙引號:

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

這樣使用單引號,並避免轉義:

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

或用於打印文字反斜杠和換行符:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM