本问题已经有最佳答案,请猛点这里访问。
如何在包含lf字符的bash cli中传递参数?比如:myprog foo
bar。
我试过这个:
myprog `printf 'foo
bar'`
myprog foo
bar
我使用这个bash程序来测试结果:
#myprog
echo $*
以及node.js程序
#!/usr/bin/env node
console.log(process.argv[2])
它不起作用。
不过我要复制这个,回答之前没查过
在bash中,使用类似于ansi c的字符串,其$'...'符号如下。当您想将特殊字符作为参数传递给某些程序时,这特别有用。
myProgram $'foo
bar'
您可以看到形成的字符串的hexdump。不要混淆后面的新行,因为它是由bash中的here字符串<<
$ hexdump -c <<< $'foo
bar'
0000000 f o o
b a r
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 |
|
| newline |
|
| 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)|
|
nn | 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) |
+-------------+----------------------------------------------------------------------------------------------------------------------------------+