linux shell 换行转义字符,在bash中传递换行符(lf)/换行符作为参数

本问题已经有最佳答案,请猛点这里访问。

如何在包含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)                                          |

+-------------+----------------------------------------------------------------------------------------------------------------------------------+

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值