(1)范例:比较双引号“ ” ,单引号‘ ’, ``三者区别
结论:
(1)单引号:六亲不认,变量和命令都不识别,都当成了普通的字符串。
(2)反向单引号:变量和命令都识别,并且会将反向单引号的内容当成命令进行执行后,再交给调用反向单引号的 命令继续。
(3)双引号:不能识别命令,可以识别变量。
代码测试过程如下:
[root@centos76-test ~]#echo "echo $HOSTNAME"
echo centos76-test.maedu.org
[root@centos76-test ~]#echo 'echo $HOSTNAME'
echo $HOSTNAME
[root@centos76-test ~]#echo `echo $HOSTNAME`
centos76-test.maedu.org
[root@centos76-test ~]#
#范例:``使用举例
[root@centos76-test ~]#echo "This system's name is $(hostname)"
This system's name is centos76-test.maedu.org
[root@centos76-test ~]#
[root@centos76-test ~]#echo "i am `whoami`"
i am root
[root@centos76-test ~]#
[root@centos76-test ~]#touch `date +%F`.txt
[root@centos76-test ~]#ll
total 0
-rw-r--r-- 1 root root 0 Nov 19 07:46 2020-11-19.txt
[root@centos76-test ~]#touch `hostname`-`date +%F`.log
[root@centos76-test ~]#ll
total 0
-rw-r--r-- 1 root root 0 Nov 19 07:46 2020-11-19.txt
-rw-r--r-- 1 root root 0 Nov 19 07:47 centos76-test.maedu.org-2020-11-19.log
[root@centos76-test ~]#
[root@centos76-test ~]#touch `date +%F_%H-%M-%S`.log
[root@centos76-test ~]#touch `date -d '-1 day' +%F`.log
[root@centos76-test ~]#ll
total 0
-rw-r--r-- 1 root root 0 Nov 19 07:48 2020-11-18.log
-rw-r--r-- 1 root root 0 Nov 19 07:48 2020-11-19_07-48-25.log
-rw-r--r-- 1 root root 0 Nov 19 07:46 2020-11-19.txt
-rw-r--r-- 1 root root 0 Nov 19 07:47 centos76-test.maedu.org-2020-11-19.log
[root@centos76-test ~]#
注意:感叹号需要在单引号中使用才有效果。
(2)范例:$( ) 和 ``
#这两个是等价的,可以互换使用;
把一个命令的输出打印给另一个命令的参数:
$(COMMAND) 或 COMMAND
#具体测试代码如下:
[root@centos76-test ~]#ll `echo `date +%F` .txt`
-bash: .txt: command not found
ls: cannot access date: No such file or directory
ls: cannot access +%F: No such file or directory
[root@centos76-test ~]#ll $(echo $(date +%F).txt)
-rw-r--r-- 1 root root 0 Nov 19 07:46 2020-11-19.txt
[root@centos76-test ~]#ll `echo $(date +%F).txt`
-rw-r--r-- 1 root root 0 Nov 19 07:46 2020-11-19.txt
[root@centos76-test ~]#ll $(echo `date +%F`.txt)
-rw-r--r-- 1 root root 0 Nov 19 07:46 2020-11-19.txt
[root@centos76-test ~]#
结束。