bash - How to pass a line feed to a script?

2019-03-02 10:37发布

Here is a simple script called command.sh:

#!/bin/bash

echo "I received: |$1|"

When I call it with a line feed, it doesn't output it:

$ ./command.sh foo\
> bar
I received: |foobar|

Why does the line feed get lost?

1条回答
Fickle 薄情
2楼-- · 2019-03-02 11:13

Call your script as:

./command.sh 'foo
> bar'

By placing \ before newline you're merely breaking current command line and not really passing newline character to your script.

If you want to do it in single line then use:

./command.sh $'foo\nbar'
查看更多
登录 后发表回答