将换行符(LF)/换行符作为参数传递给bash

时间:2017-10-23 09:27:19

标签: bash shell

如何在包含LF字符的Bash CLI中传递参数?类似于:myprog foo\nbar

我试过了:

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

我使用这个bash程序来测试结果:

#myprog
echo $*

和node.js程序

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

它不起作用。

1 个答案:

答案 0 :(得分:8)

bash中使用ANSI C like strings,并使用$'...'表示法如下。当您想要将特殊字符作为参数传递给某些程序时,这尤其有用。

myProgram $'foo\nbar'

您可以看到形成的字符串的hexdump。不要混淆尾随的新行,因为它是由<<<

中的here-string bash构造引入的
$ hexdump -c <<< $'foo\nbar'
0000000   f   o   o  \n   b   a   r  \n
0000008

还支持以下转义序列,此处更新列表,因为它在重复列表中不可用。

+-------------+----------------------------------------------------------------------------------------------------------------------------------+
|  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)                                          |
+-------------+----------------------------------------------------------------------------------------------------------------------------------+