What's the difference between $(...) and `...` in Bash

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?

2

2 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
fi

The $(pwd) works in the subshell created when I run the process.

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like