免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1145 | 回复: 1

[系统管理] bash脚本里加双引号与不加的区别 [复制链接]

论坛徽章:
1
操作系统版块每日发帖之星
日期:2016-01-20 06:20:00
发表于 2016-05-12 19:19 |显示全部楼层
本帖最后由 bobower 于 2016-05-12 19:20 编辑

话不多说,直接上示例说明:
/tmp/test]# ls
1  2  3  4  test.sh
/tmp/test]# cat test.sh
#!/bin/bash
echo `ls .`
### 重点1
/tmp/test]# sh test.sh
1 2 3 4 test.sh


/tmp/test]# vi test.sh
/tmp/test]# cat test.sh
#!/bin/bash
echo "`ls .`"
###重点2
/tmp/test]# sh test.sh
1
2
3
4
test.sh

说明:目录/tmp/test下有5个文件,其中test.sh是执行脚本,其余是随手创建的测试文件。
第1次执行test.sh,没有双引号,输出了一行。
然后修改了test.sh,加上了双引号,输出的每个文件都占一行。
疑问:只是加了一个双引号,导致输出格式发生变,不太明白其中原因,请大神帮忙解答,多谢了~!




论坛徽章:
2
操作系统版块每日发帖之星
日期:2016-05-16 06:20:0015-16赛季CBA联赛之八一
日期:2016-05-18 15:38:22
发表于 2016-05-13 16:54 |显示全部楼层
  1. Command Substitution
  2.        Command substitution allows the output of a command to replace the command name.  There are two forms:

  3.               $(command)
  4.        or
  5.               `command`

  6.        Bash  performs  the expansion by executing command and replacing the command substitution with the standard output of the
  7.        command, with any trailing newlines deleted.  Embedded newlines are not deleted, but they  may  be  removed  during  word
  8.        splitting.  The command substitution $(cat file) can be replaced by the equivalent but faster $(< file).
复制代码
执行``的时候,会把里面的换行用空格替换掉。
  1. yu@yu-vm:~/0513$ awk '{print $1}' 1.txt
  2. a
  3. b
  4. c
  5. d
  6. a
  7. b
  8. a
  9. c
  10. yu@yu-vm:~/0513$ echo `awk '{print $1}' 1.txt`
  11. a b c d a b a c
  12. yu@yu-vm:~/0513$ echo "`awk '{print $1}' 1.txt`"
  13. a
  14. b
  15. c
  16. d
  17. a
  18. b
  19. a
  20. c
  21. yu@yu-vm:~/0513$
复制代码
至于ls为啥单独执行的时候不会换行,这个man ls也没查到。期待大神来解答下
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP