在bash中将换行符(LF)/换行符作为参数传递 [英] Passing line-feed(LF)/new-line character as an argument in bash

查看:179
本文介绍了在bash中将换行符(LF)/换行符作为参数传递的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

我尝试过:

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

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

I used this bash program to test the results:

#myprog
echo $*

和node.js程序

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

它不起作用.

推荐答案

bash中,使用

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'

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

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

这篇关于在bash中将换行符(LF)/换行符作为参数传递的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆