Home > front end >  Pass a string argument including newlines to my binary in terminal of Mac OS [duplicate]
Pass a string argument including newlines to my binary in terminal of Mac OS [duplicate]

Time:09-17

I would like a pass a string argument including newlines \n or \r\n to my binary in terminal of Mac OS. I tried ./myBinary 'abc \n efg' and ./myBinary 'abc \r\n efg', they did not work.

I also tried echo 'abc \n efg' and echo 'abc \r\n efg', newlines were not displayed in the output, so I guess we need to do something around \n and \r\n to let the command line recognise them.

Could anyone help?

CodePudding user response:

You can type the newline:

./myBinary 'abc 
 def'

You can use ANSI-C quoting:

./myBinary $'abc \n def'

You can use command substitution and two echo-s, note quotes:

./myBinary "$(echo "abc "; echo " def")"
# or
./myBinary "$(printf "%s\n" "abc " " def")"
  • Related