Bash中单引号和双引号的区别

Bash单引号和双引号的区别

单引号:必须成对使用,它可以保护所有的字符不被翻译。如变量$1,和奇数个单引号的作用相同,偶数个单引号=1个双引号
双引号:必须成对出现,它可以保护一些元字符不被翻译,但允许变量和命令替换,和偶数个单引号的作用相同
反斜线\:shell也不解释转义符\后的字符,’$1’和”\$1″一样
单引号和双引号可以互相保护

例子1:

$ cat test.sh

#!/bin/bash

echo “$1″=$1 “$2″=$2
echo ‘$1’=$1 ‘$2’=$2

$ ./test.sh hello world
hello=hello world=world
$1=hello $2=world

例子2:

#!/bin/bash

echo ‘$1’=$1
echo ‘$1’=’$1’
echo ‘$1’=”$1″          //一个双引号
echo ‘$1’=$1
echo ‘$1’=’$1
echo ‘$1’=”$1”         //两个单引号

例子3:

单引号中的反斜线不被翻译
$echo ‘\\’
\\

例子4:

单引号保护双引号
$echo ‘Mother yelled, “Time to eat!” ‘
Mother yelled, “Time to eat!”

例子5:

双引号保护单引号
$echo “Hi, I’m glad to mee you”
Hi, I’m glad to meet you 

Bash函数简述  http://www.linuxidc.com/Linux/2015-06/118796.htm

Linux Bash脚本编程语言中的美学与哲学  http://www.linuxidc.com/Linux/2015-03/114436.htm

提高Linux工作效率的十大bash技巧 http://www.linuxidc.com/Linux/2015-03/114349.htm

bash的登录与欢迎信息:/etc/issue,/etc/motd  http://www.linuxidc.com/Linux/2014-08/105419.htm

Bash常用的几个配置文件 http://www.linuxidc.com/Linux/2014-08/105202.htm

Bash脚本15分钟进阶教程 http://www.linuxidc.com/Linux/2014-04/100750.htm

10个 Linux/Unix下 Bash 和 KSH shell 的作业控制实例 http://www.linuxidc.com/Linux/2014-03/98159.htm

Ubuntu下shell脚本运行异常:Bash和dash的区别 http://www.linuxidc.com/Linux/2013-10/91100.htm

Bash脚本之for语句if语句以及各种测试语句 http://www.linuxidc.com/Linux/2013-07/87922.htm

什么是Bash Shell的内建(build in)命令 http://www.linuxidc.com/Linux/2013-06/86039.htm

分享有用的 bash 别名和函数  http://www.linuxidc.com/Linux/2015-01/111178.htm

Linux Bash Shell入门教程  http://www.linuxidc.com/Linux/2013-08/8848.htm