The linux command 之引用
2024-10-07 22:20:48
[me@linuxbox ~]$ echo this is a test
this is a test
shell 会对echo进行单词分割(word splitting)去除多余的空白。
[me@linuxbox ~]$ echo The total is $100.00
The total is 00.00
因为$1是一个未定义的变量,所以参数扩展会将$1替换为空字符串。
引用的目的就是由选择性的避免不想要的扩展。
一、双引号(Double Quotes)
将text放在双引号内时,所有特殊字符的特殊含义都将消失。但是字符'$','\','‘反引号’'除外,这就意味着单词分割、路径名扩展、波浪线扩展和花括号扩展都将失效,但是参数扩展、算术扩展、和命令替换仍将生效。
[me@linuxbox ~]$ ls -l two words.txt
ls: cannot access two: No such file or directory
ls: cannot access words.txt: No such file or directory
单词分割功能会将two words.txt分割成two 和word.txt两个部分。使用双引号就可以避免这种现象出现
[me@linuxbox ~]$ ls -l "two words.txt"
-rw-rw-r-- me me -- : two words.txt
记住:参数扩展、算术扩展、命令替换在双引号内仍然有效。
[me@linuxbox ~]$ echo "$USER $((2+2)) $(cal)"
me February
Su Mo Tu We Th Fr Sa
二、单引号(Single Quotes)
如果我们想抑制所有扩展,那么使用单引号。
[me@linuxbox ~]$ echo text ~/*.txt {a,b} $(echo foo) $((2+2)) $USER
text /home/me/ls-output.txt a b foo 4 me
[me@linuxbox ~]$ echo "text ~/*.txt {a,b} $(echo foo) $((2+2)) $USER"
text ~/*.txt {a,b} foo 4 me
[me@linuxbox ~]$ echo 'text ~/*.txt {a,b} $(echo foo) $((2+2)) $USER'
text ~/*.txt {a,b} $(echo foo) $((2+2)) $USER
三、转义字符(Escaping Characters)
通常使用转义字符(backslash)放在双引号中有选择性的来进行扩展。
[me@linuxbox ~]$ echo "The balance for user $USER is: \$5.00"
The balance for user me is: $5.00
最新文章
- js对象克隆方法
- 【ZJOI2013】k大数查询 BZOJ 3110
- WinServer 2008 远程桌面连接设置
- USBD_STATUS
- 循序渐进Python3(十二) --1--  web框架之django
- mouse scrollings and zooming operations in linux &; windows are opposite
- 6/13 sprint2 看板和燃尽图的更新
- iOS_SN_百度地图基本使用(1)
- python高级编程之选择好名称:pepe8和命名最佳实践
- JS 原型 &; 继承
- google搜索指南
- ECMAScript 6 学习(一)generator函数
- Install VirtualBox Guest Additions for elementary os
- 【十二】jvm 性能调优工具之 jhat (JVM Heap Analysis Tool)
- XSplit Quality, VBV-Buffer, VBV-Maxrate and Preset Settings
- 游戏搭服自动化脚本shell
- 2018-2019 2 20165203 《网络对抗技术》Exp6 信息搜集与漏洞扫描
- LOJ.6284.数列分块入门8(分块)
- 【译】SSH隧道:本地和远程端口转发
- 在eclipse中的maven工程中执行maven命令的步骤