Skip to content

Instantly share code, notes, and snippets.

@Siddhant-K-code
Created September 4, 2024 02:02
Show Gist options
  • Save Siddhant-K-code/75cf74c28ad646d4656d1941dad112e1 to your computer and use it in GitHub Desktop.
Save Siddhant-K-code/75cf74c28ad646d4656d1941dad112e1 to your computer and use it in GitHub Desktop.
Linux: Difference Between Single Quotes, Double Quotes, and Backquotes

1. Introduction

In Linux, single quotes ('), double quotes ("), and backquotes (`) serve different purposes. The easiest way to understand their differences is by using the DATE variable, the "date" string, and the date command.

[Difference in Quotation Marks]

Important:

  • ' ... Single quotes
    • Everything within single quotes is interpreted as a string (ignoring any variables).
  • " ... Double quotes
    • Contents within double quotes are generally interpreted as strings. However, if a variable is included, it's referenced.
  • ` ... Backquotes
    • Contents within backquotes are interpreted as commands.

2. Preparation for Comparison

First, let's look at the date command, which prints the current date and time:

user@host:~$ date
Wed Sep  4 07:30:38 IST 2024

Next, we'll define the DATE variable and assign the "date" string to it:

user@host:~$ DATE=date

We can reference a variable by prefixing it with $. Let's verify the variable definition using the echo command:

user@host:~$ echo $DATE
date

3. Comparing Quotes

Now, let's examine the output differences based on the quotation marks used.

3-1. ' ... Single Quotes

Everything within single quotes is interpreted as a string:

user@host:~$ echo '$DATE'
$DATE

The $DATE is treated as a literal string, not a variable reference.

3-2. " ... Double Quotes

Within double quotes, contents are generally interpreted as strings, but variables are referenced:

user@host:~$ echo "$DATE"
date

Since "date" was assigned to the DATE variable, the output is "date".

3-3. ` ... Backquotes

Contents within backquotes are interpreted as commands:

user@host:~$ echo `$DATE`
Wed Sep  4 07:31:18 IST 2024

This might be confusing, but here's what happens:

  1. $DATE is referenced, resulting in "date"
  2. The "date" command is then executed within the backquotes
  3. The output of the date command is then echoed

Bonus

To deepen your understanding, let's try combining techniques:

user@host:~$ echo "Today's date is `date`"
Today's date is Wed Sep  4 07:32:06 IST 2024
user@host:~$ echo 'Today's date is `date`'
Today's date is `date`

Notice the difference? In the second example with single quotes, everything is interpreted as a string, so the date command isn't executed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment