linux系统中常用的转义字符 \、" "、' '、` `。

1、创建测试数据

[root@linuxprobe test]# echo "aaa.bbb.ccc.ddd" > a.txt
[root@linuxprobe test]# cat a.txt
aaa.bbb.ccc.ddd

 

2、利用\进行转义,使转义字符仍为字符串

[root@linuxprobe test]# sed 's/./ /' a.txt ## 利用sed命令进行替换,将.替换为空格, .没有转义前表示任意字符,仅第一个a被替换为空格
 aa.bbb.ccc.ddd
[root@linuxprobe test]# sed 's/\./ /' a.txt ## 加转义字符\,.为普通字符串,被替换为空格
aaa bbb.ccc.ddd
[root@linuxprobe test]# sed 's/\./ /g' a.txt ## 加g表示全局替换,所有的.都被替换为空格
aaa bbb ccc ddd

 

3、' '将变量保持为普通字符串," "保持变量的原有属性

[root@linuxprobe test]# ls
a.txt
[root@linuxprobe test]# cat a.txt
aaa.bbb.ccc.ddd
[root@linuxprobe test]# x=5  ##自定义变量 x等于5
[root@linuxprobe test]# echo $x  ##查看变量
5
[root@linuxprobe test]# sed 's/b/$x/g' a.txt  ## ' '将变量保持为普通字符串,所有的b被替换为$x
aaa.$x$x$x.ccc.ddd
[root@linuxprobe test]# sed "s/b/$x/g" a.txt ## " " 保持变量原有属性,所有的b被替换为5
aaa.555.ccc.ddd

 

4、` ` 表示返回命令执行后的结果

[root@linuxprobe test]# for i in `seq 5`;do echo $i;done  ## 将seq 5执行的结果返回给变量i
1
2
3
4
5
[root@linuxprobe test]# ls
a.txt
[root@linuxprobe test]# find *
a.txt
[root@linuxprobe test]# find * | sed "s#^#`pwd`/#"  ## 在a.txt文件前加绝对路径,利用sed在a.txt前添加pwd执行后的结果
/home/linuxprobe/data/sheepreq/fasta/test/test/a.txt

 

posted @ 2020-10-05 13:32  小鲨鱼2018  阅读(16058)  评论(0编辑  收藏  举报