bash-如何将换行符传递给脚本? [英] bash - How to pass a line feed to a script?

查看:81
本文介绍了bash-如何将换行符传递给脚本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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|

为什么换行符会丢失?

推荐答案

调用脚本为:

./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'

这篇关于bash-如何将换行符传递给脚本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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