反引号与$()中的反斜杠要特别注意

在程序中经常会遇到将linux命令运行结果赋值给变量的情况,但是碰到反斜杠时要特别注意,推荐使用$()

[root@node0xa1 /]# strtest="\1\2\3\4"
[root@node0xa1 /]# result=`echo $strtest|awk -F"\\" '{print $2}'`  
bash: command substitution: line 1: unexpected EOF while looking for matching `"'
bash: command substitution: line 2: syntax error: unexpected end of file
[root@node0xa1 /]# result=$(echo $strtest|awk -F"\\" '{print $2}')
[root@node0xa1 /]# echo $result
1

[root@node0xa1 /]# result=`echo $strtest|awk -F'\' '{print $2}'`
[root@node0xa1 /]# echo $result
1
[root@node0xa1 /]# result=$(echo $strtest|awk -F'\' '{print $2}')
[root@node0xa1 /]# echo $result
1

你可能感兴趣的:(反引号与$()中的反斜杠要特别注意)