シェルスクリプトで外部コマンドを利用する場合の注意点

November 10, 2014

TL;DR

シェルスクリプトでbackquoteを見つけたら
オールドスタイルおじさんを探し、矯正させよう

GNU Bash-2.05 manual

       When the old-style backquote form of substitution is used, backslash retains its  lit-  
       eral  meaning except when followed by $, `, or \.  The first backquote not preceded by  
       a backslash terminates the command substitution.  When using the $(command) form,  all  
       characters between the parentheses make up the command; none are treated specially.
#!/bin/bash

#!/bin/bash

a=`echo '\'`
echo ${a} # \

b=`echo "\\"`
echo ${b} # unexpected EOF while looking for matching `"'

c=$(echo '\')
echo ${c} # \

d=$(echo "\\")
echo ${d} # \

参考

http://hyperpolyglot.org/unix-shells#cmd-subst-note

追記