Difference between using or not using Back Quote

53 Views Asked by At

What is the difference of using or not using the BACK QUOTE `

For example, both the code works regardless.

First example with the BACK QUOTE, second example without BACK QUOTE.

Thank you so much in advance for your help.

if [ "`/usr/bin/whoami`" != "root" ] ; then
/bin/echo "This script must be run as root or sudo."
exit 0
fi

if [ "/usr/bin/whoami" != "root" ] ; then
/bin/echo "This script must be run as root or sudo."
exit 0
fi
1

There are 1 best solutions below

1
Romeo Ninov On BEST ANSWER

In first case you compare in if the result of execution of command /usr/bin/whoami (this is what backticks do)

In second case you compare two strings

/usr/bin/whoami

and

root

one more example can be:

if [ "`date`" = "date" ]
then echo this is true
fi

the above code will NOT work because you compare string "Thu Jan 30 17:03:54 CET 2020" and string "date"

if [ "date" = "date" ]
then echo this is true
fi

the above code will work :)