Literal string

Updated: 12/31/2022 by Computer Hope

A literal string may refer to any of the following:

1. Alternatively called literal(s), a literal string is several characters enclosed in double or single quotes, depending on the programming language or command line. A program does not interpret characters in a literal string until it encounters the next double or single quote.

For example, in Perl using the command below, it would print "Hello World! Here is a $1."

print 'Hello World! Here is a $1';

However, surrounding the string with double quotes instead of single quotes causes Perl to try interpreting the $1 as a variable. Escaping the dollar sign results in Perl treating the dollar sign as a literal character, as shown below.

print "Hello World! Here is a \$1";

The same is true to add a variable to a string. In the following example, the variable $name is assigned "Nathan", and the first print using a single quote is treated as a literal string, which means it prints "Hello $name". However, the second print with double quotes is an interpreted string and would print "Hello Nathan".

use strict;
my $name = "Nathan";
print 'Hello $name';
print "Hello $name";
Tip

For literal and interpreted strings, if you need to insert a quote, it must be escaped if it's the same quote used to start the string.

2. A literal character is a single character that's not interpreted. For example, the * (asterisk) is often treated as a wildcard in searches. To get the literal character would require it to be escaped with another character.

3. When connected to an FTP session, the literal command sends a literal string to the connected computer and expects a one-line response.

Machine language, Programming terms, Variable