1. 程式人生 > 實用技巧 >linux系統中常用的轉義字元 \、" "、' '、` `。

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