bash - 如何将换行符传递给脚本?

Ste*_*han 1 linux bash arguments linefeed

这是一个名为command.sh的简单脚本:

#!/bin/bash

echo "I received: |$1|"
Run Code Online (Sandbox Code Playgroud)

当我用换行符调用它时,它不会输出它:

$ ./command.sh foo\
> bar
I received: |foobar|
Run Code Online (Sandbox Code Playgroud)

为什么换行会丢失?

anu*_*ava 5

将您的脚本称为:

./command.sh 'foo
> bar'
Run Code Online (Sandbox Code Playgroud)

通过\在换行符之前放置,您只是打破当前命令行而不是真正将换行符传递给您的脚本.

如果你想用单行做,那么使用:

./command.sh $'foo\nbar'
Run Code Online (Sandbox Code Playgroud)