Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

bash printf backslash then new line

Tags:

linux

bash

I am trying to create this from bash into a c 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"

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

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

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

Is there any better way to handle this?

like image 546
xsong Avatar asked Mar 19 '26 02:03

xsong


1 Answers

Use hexadecimal escape sequences:

FORMAT="%-4s %-32s \x5C\n"
like image 175
Walker Mills Avatar answered Mar 20 '26 17:03

Walker Mills



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!