What's the Difference Between $(. ..) and `. .. ` in Bash

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

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

Sarah Jenkins
Author

Sarah Jenkins

Sarah Jenkins is a veteran tech journalist with over 12 years of experience covering artificial intelligence, mobile innovations, and digital ethics. Her insights have appeared in leading technology publications worldwide.