linux shell 字符串变量 有双引号和无双引号的区别

发布时间 2023-09-12 23:38:57作者: 小鲨鱼2018

 

001、

[root@pc1 test02]# ls
a.sh  b.sh
[root@pc1 test02]# cat a.sh         ## 测试程序1
#!/bin/bash

str1="ab_cd_ef"
tmp1=$(echo $str1 | sed 's/_/\n/g')
echo $tmp1
[root@pc1 test02]# cat b.sh         ## 测试程序2
#!/bin/bash

str1="ab_cd_ef"
tmp1=$(echo $str1 | sed 's/_/\n/g')
echo "$tmp1"
[root@pc1 test02]# diff a.sh b.sh
5c5
< echo $tmp1
---
> echo "$tmp1"
[root@pc1 test02]# bash a.sh
ab cd ef
[root@pc1 test02]# bash b.sh     ## 加双引号,可以输出换行符
ab
cd
ef