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

来源:互联网 发布:卡密自动发货源码 编辑:程序博客网 时间:2024/05/19 17:50

先看下面的命令:

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

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

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

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

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

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

0 0