bashbackquote

Difference between using or not using Back Quote


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

Solution

  • 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 :)