LPI Linux Essentials Variable Question

Have a look at this question on the screenshot.  
I don't understand why my answer is wrong when, practically it works.  If I type at the console:
hit enter and then type up:
The date prints to standard output like this
So why is it, the answer to the question is:

  • post-author-pic
    Kyle J

    Not certain myself, but it could be that because they want to force you to use the quotes as it is less error-prone and more convenient.

    E.g displaying the hour only, with quotes and without quotes:
       actdat='date %H'     vs     actdat=date\ '%H'

  • post-author-pic
    Jake W

    I can't tell for sure due to the photo's resolution, but those look like backticks rather than single quotes. The problem with both methods you described:



    is that while both will work when invoked by themselves, they will not evaluate the value and store it in the variable.


    [cloud_user@jwickenhofer4c ~]$ actdate=date

    [cloud_user@jwickenhofer4c ~]$ $actdate

    Mon Dec 24 09:09:53 EST 2018

    [cloud_user@jwickenhofer4c ~]$ echo $actdate



    When we pass the variable by itself, bash just sees the date command and runs it. However, when we pass it to another command, it just sees the string value "date." Backtick expansion, by contrast, will actually cause the contents between the backticks to be evaluated every time the variable is referenced:


    [cloud_user@jwickenhofer4c ~]$ actdate=`date`

    [cloud_user@jwickenhofer4c ~]$ echo $actdate

    Mon Dec 24 09:12:59 EST 2018


    The question still isn't 100% correct, however, because backtick expansion is actually deprecated, and parenthetical expansion is the correct way to utilize command substitution, like so:


    [cloud_user@jwickenhofer4c ~]$ actdate=$(date)

    [cloud_user@jwickenhofer4c ~]$ echo $actdate

    Mon Dec 24 09:16:34 EST 2018


    Backticks will still work but I've had some older scripts where certain things did not work correctly, and when changed to parentheses, worked perfectly.

Looking For Team Training?

Learn More