如何在包含LF字符的Bash CLI中传递参数?就像是:myprog foo\nbar
我试过这个:
myprog `printf 'foo\nbar'`
myprog foo\nbar
Run Code Online (Sandbox Code Playgroud)
我使用这个bash程序来测试结果:
#myprog
echo $*
Run Code Online (Sandbox Code Playgroud)
和node.js程序
#!/usr/bin/env node
console.log(process.argv[2])
Run Code Online (Sandbox Code Playgroud)
这是行不通的.
在bash使用ANSI C之类的字符串,其$'...'符号如下.当您想要将特殊字符作为参数传递给某些程序时,这尤其有用.
myProgram $'foo\nbar'
Run Code Online (Sandbox Code Playgroud)
你可以看到hexdump形成的字符串.不要混淆尾随的新行,因为它是由here-string <<<构造引入的bash
$ hexdump -c <<< $'foo\nbar'
0000000 f o o \n b a r \n
0000008
Run Code Online (Sandbox Code Playgroud)
还支持以下转义序列,在此更新列表,因为它在重复的列表中不可用.
+-------------+----------------------------------------------------------------------------------------------------------------------------------+
| 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) |
+-------------+----------------------------------------------------------------------------------------------------------------------------------+
Run Code Online (Sandbox Code Playgroud)