linux中单引号和双引号的区别是什么

区别:单引号属于强引用,它会忽略所有被引起来的字符的特殊处理,被引用起来的字符会被原封不动的使用;而双引号属于弱引用,它会对一些被引起来的字符进行特殊处理。简单来说,单引号直接输出内部字符串,不解析特殊字符;双引号内则会解析特殊字符。

本教程操作环境:CentOS 6系统、Dell G3电脑。

1、单引号

单引号属于强引用,它会忽略所有被引起来的字符的特殊处理,被引用起来的字符会被原封不动的使用,唯一需要注意的点是不允许引用自身;

单引号将其中的内容都作为了字符串来,忽略所有的命令和特殊字符,类似于一个字符串用法

echo 'This is a string'
>>> This is a string
echo 'ls ./'
>>> ls ./

2、双引号

双引号属于弱引用,它会对一些被引起来的字符进行特殊处理。

双引号与单引号的区别在于其可以包含特殊字符(单引号直接输出内部字符串,不解析特殊字符;双引号内则会解析特殊字符),包括', , $, \,如果要忽略特殊字符,就可以利用\来转义,忽略特殊字符,作为普通字符输出

var = 1
echo '$var'
>>> $var
echo $var
>>> 1

echo Here 'this is a string' is a string
>>> Here 'this is a string' is a string
echo Here \this is a string\ is a string
>>> Here this is a string is a string

3、反引号

反引号用来包含一个命令字符串的,其中的命令会先执行,得到的结果会返回到层命令再执行:

echo `echo 'this is the inner string'`+'out' 
>>> this is the inner string+out
echo `echo 'this is the inner \` string'`+'out'    #转义反引号
>>> this is the inner ` string+out

反引号类似与$(command)类似。

#一个使用例子,如果想要遍历当前文件夹及其一级子文件夹:
ls $(ls) 
ls `ls`
>>> first_folder
>sub_1 ..sub_2
> second_folder
>sub_1 ..sub_2
>

相关推荐:《Linux教程

相关文章

Linux中的ARP防火墙主要用于防御ARP欺骗攻击,其效果取决于多...
insmod和modprobe加-f参数导致Invalid module format错误 这...
将ArchLinux安装到U盘 几个月前入门Arch的时候上网搜了不少安...
1、安装Apache。 1)执行如下命令,安装Apache服务及其扩展包...
一、先说一下用ansible批量采集机器信息的实现办法: 1、先把...
安装配置 1. 安装vsftpd 检查是否安装了vsftpd # rpm -qa | ...