方式1:$(命令)
例1:输出的文本中使用$(pwd)打印当前的路径
[root@kedacom test]# pwd/opt/test[root@kedacom test]# echo "The current directory is $(pwd)"The current directory is /opt/test[root@kedacom test]# cd[root@kedacom ~]# echo "The current directory is $(pwd)"The current directory is /root
例2:创建的文件名称带上日期$(date +%Y-%m-%d-%H-%M-%S)
[root@kedacom test]# date +%Y-%m-%d-%H-%M-%S2022-01-31-15-48-50[root@kedacom test]# touch file-$(date +%Y-%m-%d-%H-%M-%S)[root@kedacom test]# lsfile-2022-01-31-15-48-54[root@kedacom test]# touch file-$(date +%Y-%m-%d-%H-%M-%S)[root@kedacom test]# lsfile-2022-01-31-15-48-54 file-2022-01-31-15-48-58
方式2:命令
touch file-date +%Y-%m-%d-%H-%M-%S可以实现上面touch file-$(date +%Y-%m-%d-%H-%M-%S)的功能
[root@kedacom test]# lsfile-2022-01-31-15-48-54 file-2022-01-31-15-48-58[root@kedacom test]# touch file-`date +%Y-%m-%d-%H-%M-%S`[root@kedacom test]# lsfile-2022-01-31-15-48-54 file-2022-01-31-15-48-58 file-2022-01-31-15-57-14
强引用’’与弱引用””
只有在弱引用中才能实现命令替换,强引用中不能实现命令替换
#弱引用[root@kedacom test]# echo "The current directory is $(pwd)"The current directory is /opt/test#强引用[root@kedacom test]# echo 'The current directory is $(pwd)'The current directory is $(pwd)#弱引用[root@kedacom test]# touch "file-`date +%Y-%m-%d-%H-%M-%S`"[root@kedacom test]# lsfile-2022-01-31-16-05-01#强引用[root@kedacom test]# touch 'file-`date +%Y-%m-%d-%H-%M-%S`'[root@kedacom test]# lsfile-`date +%Y-%m-%d-%H-%M-%S`
