6

How to pass an argument in Bash CLI that contains an LF character? Something like: myprog foo\nbar

I tried this:

myprog `printf 'foo\nbar'`
myprog foo\nbar

I used this bash program to test the results:

#myprog
echo $*

and node.js program as well

#!/usr/bin/env node
console.log(process.argv[2])

It does not work.

Inian
  • 80,270
  • 14
  • 142
  • 161
exebook
  • 32,014
  • 33
  • 141
  • 226

1 Answers1

20

In bash use the ANSI C like strings, with the $'...' notation as below. This is especially useful when you want to pass special characters as arguments to some programs.

myProgram $'foo\nbar'

You can see the hexdump of the string formed. Don't confuse the trailing new line, since it is introduced by the here-string <<< construct in bash

$ hexdump -c <<< $'foo\nbar'
0000000   f   o   o  \n   b   a   r  \n
0000008

The following escape sequences are also supported, updating the list here, since it is not available in the duplicated one.

+-------------+----------------------------------------------------------------------------------------------------------------------------------+
|  code       |    meaning                                                                                                                       |
|             |                                                                                                                                  |
+-------------+----------------------------------------------------------------------------------------------------------------------------------+
|  \"         | double-quote                                                                                                                     |
|  \'         | single-quote                                                                                                                     |
|  \\         | backslash                                                                                                                        |
|  \a         | terminal alert character (bell)                                                                                                  |
|  \b         | backspace                                                                                                                        |
|  \e         | escape (ASCII 033)                                                                                                               |
|  \E         | escape (ASCII 033) \E is non-standard                                                                                            |
|  \f         | form feed                                                                                                                        |
|  \n         | newline                                                                                                                          |
|  \r         | carriage return                                                                                                                  |
|  \t         | horizontal tab                                                                                                                   |
|  \v         | vertical tab                                                                                                                     |
|  \cx        | a control-x character, for example, $'\cZ' to print the control sequence composed of Ctrl-Z (^Z)                                 |
|  \uXXXX     | Interprets XXXX as a hexadecimal number and prints the corresponding character from the character set (4 digits) (Bash 4.2-alpha)|
|  \UXXXXXXXX | Interprets XXXX as a hexadecimal number and prints the corresponding character from the character set (8 digits) (Bash 4.2-alpha)|
|  \nnn       | the eight-bit character whose value is the octal value nnn (one to three digits)                                                 |
|  \xHH       | the eight-bit character whose value is the hexadecimal value HH (one or two hex digits)                                          |
+-------------+----------------------------------------------------------------------------------------------------------------------------------+
Inian
  • 80,270
  • 14
  • 142
  • 161