【发布时间】:2022-12-12 12:21:10
【问题描述】:
$ cat hello.txt
shreenivasa hi hello this is test mail
shreenivasa how are you man
如果我在终端运行
$ cat hello.txt | grep shreenivasa | cut -d ' ' -f 2-
它给出以下输出
hi hello this is test mail
how are you man
但是如果我把它写成hello.sh中的脚本,如下所示
#!/bin/bash
hi=`cat hello.txt | grep shreenivasa | cut -d ' ' -f 2-`
echo $hi
./hello.sh 的输出是
hi hello this is test mail how are you man.
我想在下一行打印“你好吗” 喜欢
hi hello this is test mail
how are you man
试过 $hi\n 没用
【问题讨论】:
-
请查看how do I format my posts 然后(重新)格式化问题
-
首先,因为您使用的是
grep,所以不需要使用cat。 Grep 读取文件。 (见en.wikipedia.org/wiki/Cat_(Unix)#Useless_use_of_cat) -
尝试用双引号将变量引用括起来:
echo "$hi";详情请见difference between single and double quotes in bash
标签: bash