红联Linux门户
Linux帮助

如何保存grep结果里面的换行符?

发布时间:2016-05-08 10:18:05来源:linux网站作者:CaspianSea

先看下面的命令:

~$ grep bash /etc/passwd 
root:x:0:0:root:/root:/bin/bash 
guest-rwf0fx:x:116:125:Guest,,,:/tmp/guest-rwf0fx:/bin/bash 


但是,如果把结果保存到变量里面:

~$ result=$(grep  bash /etc/passwd) 
charles@xiaotao:~$ echo $result 
root:x:0:0:root:/root:/bin/bash :116:125:Guest,,,:/tmp/guest-rwf0fx:/bin/bash 


可以看到,换行符没有了。


加上双引号可以把换行符保留下来:

$ echo "$result" 
root:x:0:0:root:/root:/bin/bash 
guest-rwf0fx:x:116:125:Guest,,,:/tmp/guest-rwf0fx:/bin/bash


本文永久更新地址:http://www.linuxdiyf.com/linux/20439.html