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

旧城冷巷雨未停 提交于 2019-12-20 05:16:06

问题


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:


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'


来源:https://stackoverflow.com/questions/38642312/bash-how-to-pass-a-line-feed-to-a-script

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!