Backticks

The final set of quotes is what we refer to as the backticks. Backticks are exceptionally useful. We're going to use them repeatedly in our scripting.

The purpose of a backtick is to be able to run a command, and capture the output of that command. Say:

DATE=`date`
            

We're assigning to the variable DATE, the output of the date command. We use backticks to do that.

Now run the following:

echo $DATE
Tue Jan 13 23:35:34 GMT 2004
            

You'll get the date that the system had at the time that you assigned that variable. Notice if you run the echo command again, the value of the DATE variable stays the same.

This is because the variable DATE was set once - when you ran the command inside the backticks.

You would have to re-set the variable in order for the date to be changed.

We can run any number of commands inside backticks. For example, in our date command above, I may only want the hours, the minutes and the seconds, rather than the entire date. How would one do this?

TIME=`date |cut -d' ' -f4`
            

We are setting our delimiter to be a space. Perhaps we want to get snazzier? Of course we want to get snazzier, we're Linux people!!!!

In the next example I will re-set my DATE variable first. What we don't want to do is to run the same command repeatedly to get the same type of information.

DATE=`date`
            

Once we've run the date command, we'll have all the information we need; the date, the time, the time zone and the year.

So instead of running the date command a second time to get the time, we will do the following:

TIME=`echo $DATE | cut -d' ' -f4`
            

Apart from anything else, it makes our script a lot more accurate. If we run the date command twice, there will be a time discrepancy (albeit small) between the first and second time the command was run, resulting in inaccurate output.

To deliver results more accurately, we run the date command once, and operate on the value of the DATE variable.

What if I want the time zone, which is the fifth field in the output of the date command?[18]

ZONE=`echo  $DATE|cut  -d " "  -f5`
            

How many commands can we put in backticks? The answer is: many. Assigned to a variable is not imperative, but it would make no sense if we just put something in backticks without an assignment.

Let's try that: Instead of assigning it to a variable just type:

`echo $DATE|cut -d" " -f5`
            

would produce:

bash: SAST: command not found
            

The output of this command produced the output 'SAST' (South African Standard Time). Output was produced at the command prompt, which tried to run the command SAST, which of course is not a command. So the system returns an error message.

So our backticks can be used very effectively in scripts. In our previous script, called mydisk.sh, we assigned a value to the variable DATE manually. Using backticks, we can now get the script to automatically assign it for us! Equally previously, we could only print the value of the df command, now we can assign values to those variables.

Before we move on from this, there's another construct that's equivalent to the backtick. Often the backticks are difficult to see when you're looking at a script. Thus there's another equivalent construct.

$(	)
            

Now, don't get this confused with $(()) which we used in arithmetic expressions. Instead of running:

DATE=`date`
            

we could've used:

DATE=$(date)
            

Exercises:

  1. Obtain the following from the uptime command

    1. NUMUSERS

    2. UPTIME

  2. Looking in the /proc/cpuinfo, set variables for:

    1. MODELNAME

    2. CPUSPEED

    3. CPUCACHE

    4. BOGOMIPS

  3. What do the following commands produce?

    1. echo "Today is the `date +"%j"` of the year"

    2. echo 'Today is the `date +"%j"` of the year'

    3. DT=`date +"%A, %e %B, %Y"`; echo '$DT'

    4. DT=`date +"%A, %e %B, %Y"`; echo "The date today is: \$DT"

    5. DT=`date +"%A, %e %B, %Y"`; echo "The date today is: $DT"

I would personally use the second option, because it is easier to read, and not as confusing.



[18] Notice I used double quotes for my delimiter (the space), I could've equally used ticks if I wanted to.