For years I've preferred to use
$(command)
rather than
`command`
while shell scripting.
For me it was mostly a matter of style, but I pretty much thought that the two were equivalents.
I felt it was a bit easier to see $(...) than `...` and sometimes I need to nest commands with:
$(command $(othercommand))
which would be impossible with backticks.
THEY ARE NOT THE SAME!
#!/bin/ksh
#
#
#
for STRING in 'sample.txt' 'File: C:\\path\\to\\file.txt|Something else'
do
printf "result: "
echo "$STRING" | sed 's/^[^|]*\\\([^\\]*\)|.*$/\1/g'
echo 1
VAR_1=$(echo "$STRING" | sed 's/^[^|]*\\\([^\\]*\)|.*$/\1/g')
echo 2
VAR_2=`echo "$STRING" | sed 's/^[^|]*\\\([^\\]*\)|.*$/\1/g'`
echo 3
VAR_3=`echo "$STRING" | sed "s/^[^|]*\\\([^\\]*\)|.*$/\1/g"`
echo 4
VAR_4=`echo "$STRING" | sed 's/^[^|]*\\\\\([^\\]*\\)|.*$/\1/g'`
cat <<EOT
VAR_1: '$VAR_1'
VAR_2: '$VAR_2'
VAR_3: '$VAR_3'
VAR_4: '$VAR_4'
EOT
done
Don't blame ksh, it does the same in bash and Bourne shell.
(although $( ) syntax is not valid in Bourne shell)
Solaris:
$ uname -a
SunOS umurxd04 5.10 Generic_137138-09 i86pc i386 i86pc
$ ./try.sh
result: sample.txt
1
2
sed: command garbled: s/^[^|]*\\([^\]*\)|.*$/\1/g
3
4
VAR_1: 'sample.txt'
VAR_2: ''
VAR_3: 'sample.txt'
VAR_4: 'sample.txt'
result: file.txt
1
2
sed: command garbled: s/^[^|]*\\([^\]*\)|.*$/\1/g
3
4
VAR_1: 'file.txt'
VAR_2: ''
VAR_3: ''
VAR_4: 'file.txt'
Linux:
$ ./try.sh
sample.txt
1
2
sed: -e expression #1, char 27: Unmatched ) or \)
3
4
VAR_1: 'sample.txt'
VAR_2: ''
VAR_3: 'sample.txt'
VAR_4: 'sample.txt'
file.txt
1
2
sed: -e expression #1, char 27: Unmatched ) or \)
3
4
VAR_1: 'file.txt'
VAR_2: ''
VAR_3: ''
VAR_4: 'file.txt'
More testing:
ksh $ echo `echo '\\'`
\
ksh $ echo `echo "\\"`
"
ksh $ echo `echo "\\\"`
\
ksh $ echo $(echo '\\')
\
ksh $ echo $(echo "\\")
\
ksh $ echo $(echo "\\\")
>
> ^C
bash $ echo `echo '\\'`
\
bash $ echo `echo "\\"`
bash: command substitution: line 1: unexpected EOF while looking for matching `"'
bash: command substitution: line 2: syntax error: unexpected end of file
bash $ echo `echo "\\\"`
\
bash $ echo $(echo '\\')
\\
bash $ echo $(echo "\\")
\
bash $ echo $(echo "\\\")
>
>