I got expected result with `...`, but failed with $(...)
kill $(ps ux | grep S..\.tcl | grep -v grep | awk '{print $2}')
Illegal variable name.
kill `ps ux | grep S..\.tcl | grep -v grep | awk '{print $2}'`
(kill expected processes)By googling, I found some says they are interchangable, but it is not by this example. So, What's the difference between $(...) and `...` in Bash?
22 Answers
I did a
grep -al 'Illegal variable name' /bin/*and found the message in /bin/csh. Looks like you are running csh not bash when you are giving the command. eg:
csh $ echo `echo abc`
abc
csh $ echo $(echo abc)
Illegal variable name. 2 Some explanation about the backtick ( ` ) also called command substitution:
In bash (git-bash) I also obtain different results. Running a script in a file, say /c/pathToScript/test.sh
#!/bin/bash
WORK_DIR=/c/myWorkingDirPath;
cd $WORK_DIR;
if [ `pwd` != $WORK_DIR ]; then echo "Oh my... "; # `pwd` returns /c/pathToScript
fi
if [ $(pwd) != $WORK_DIR ]; then echo "Thank Goodness... "; #$(pwd) returns /c/myWorkingDirPath
fiThe $(pwd) works in the subshell created when I run the process.