1. Introduction

The Linux operating system (OS) provides command-line utilities that enable us to execute various commands to perform tasks, such as managing date and time. However, some commands look similar, potentially leading to confusion, especially when the only difference between them is the type of quotes used.

In this tutorial, we’ll discuss several different commands: date, echo date, echo “`date`”, and echo ‘`date`’. While the first two commands are fairly straightforward, the latter two may require more time to understand their syntax better. By finding out how these commands differ, we can generalize our knowledge and apply it with any command.

2. The echo and date Commands

The two main commands that we combine are date and echo. Each has its own function, which we’ll briefly overview.

2.1. echo

The echo command outputs its argument to the standard output by default:

$ echo date
date

When the argument is a simple string literal like date, we see that as the result of the command. Even if we use single (‘ ‘) or double (” “) quotes, the result remains the same:

$ echo 'date'
date
$ echo "date"
date

Basically, the word date doesn’t have any special meaning when using echo.

2.2. date

When we execute the date command, it displays the day, date, time, timezone, and year on the terminal:

$ date 
Tue Sep 26 08:17:51 AM PDT 2023

However, the functionality of the date command isn’t limited to this alone. We can use it to see the date and time from a different timezone and display them in various formats. Furthermore, we can show the date from any past or future day or use it as a timestamp for files, such as creating or backing up with timestamps.

To get the same result with echo and date, we can use command substitution:

$ echo $(date)
Tue Sep 26 08:17:51 AM PDT 2023

Now, let’s see how introducing backticks and nesting them within quotes works.

3. Understanding echo “`date`” and echo ‘`date`’ Command

Backticks are used primarily for command substitution just like $(). When we enclose a command in backticks, it gets executed and the whole expression is replaced by the result, which can be used  just like any other value.

3.1. echo “`date`”

Let’s see how combining echo and date with backticks (` `) and double quotes (” “) works:

$ echo "`date`"
Tue Sep 26 08:30:28 AM PDT 2023

Here, we run the date command as the inner command, and the double quotes treat most of the output, including any whitespace, as plain text. After that, the entire command, including the double quotes, becomes the argument to echo. Thus, we get the current date and time on the standard output.

As we saw, we can also use the dollar notation $() as an alternate method for command substitution:

$ echo "$(date)"
Wed Sep 27 01:44:07 PM PDT 2023

Although both methods provide similar results, the standard is the $() operator.

Effectively, the result from this construct doesn’t differ from the basic output of date alone.

3.2. echo ‘`date`’

Let’s understand the output of the command above but with single (‘ ‘) instead of double quotes:

$ echo '`date`'
`date`

In this case, the single quotes prevent the backticks from performing command substitution. In other words, the single quotes treat the text `date` as a literal string rather than a command.

This is due to the difference between what double quotes and single quotes consider special characters.

4. Comparison

Let’s summarize our findings.

First, the date command prints the current date and time for the selected timezone, while the echo command prints its argument after any interpolation.

Next, the double quotes in echo “`date`” are the weak quotes that allow variable expansion and command substitution, so we see the same as by just using date, i.e., the current date and time.

On the other hand, the single quotes in the echo ‘`date`’ command interpret the command as a literal string and prevent it from being interpolated. Thus, any special characters inside the single quotes, like backticks or dollar notation, become regular characters that we see in the output.

Double quotes are generally recommended where variable expansion and command substitutions are required. On the other hand, single quotes are a way to prevent these features from activating.

5. Conclusion

In this article, we discussed and highlighted the differences between dateecho date, echo “`date`”, and echo ‘`date`’.

The date and the echo “`date`” commands provide similar results but differ in how they work. We also discussed the dollar notation $() method, recommended for better clarity and consistency in scripts. On the other hand, the echo ‘`date`’ command gives us different results as it doesn’t allow command substitution and treats its contents as a literal string.

Understanding the difference between double and single quotes is essential to execute commands properly and to get the desired results.

Comments are closed on this article!