Specially defined characters are essential for many of the Linux shell’s powerful features, such as filename completion and command substitution. The shell interprets these characters in a different way from other, regular characters on the command line. However, if you want to disable these characters, you can use a process called quoting.
Filename completion characters
Filename completion characters are metacharacters that enable you to abbreviate filenames or directory names. This saves time and lets you process files selectively, even if you don’t know their full names or locations.
Commonly used filename completion characters are included in the table below.
Filename completion characters
The * character (asterisk) is the most frequently used file completion character. You can use the string b*, for example, to match all the filenames beginning with the letter “b”. You can also use multiple asterisks to define a file. For example, *xx*.gif retrieves any filename that contains “xx” anywhere in its name and that has the .gif extension.
The ? character (question mark) represents any single character, so the string ??? refers to all files with a three-letter name. This special character is more restrictive than the asterisk, because it requires the character to be present. For example, the code file?.txt signifies all files that begin with “file”, include one additional character, and have the .txt extension. So the filename “file1.txt” matches, but “file.txt” and “file01.txt” don’t.
Using  characters (square brackets) allows for a more selective approach to retrieving files. The string [abc] will find files a, b, and c only. You can include a hyphen between characters inside the brackets to match a continuous range of characters. To specify the characters 0 through 9, for example, it is a lot easier and less time-consuming to type [0-9] rather than . If the brackets precede an asterisk, as in [0-9]*, filenames starting with any numbers between 0 and 9 are found. The pattern *[0-9] will match filenames ending with numbers between 0 and 9. Inserting an exclamation mark inside the brackets, as in [!b], will invert the pattern, matching any characters or ranges not specified in the brackets but ignoring those that are.
The $ (dollar sign) is used to identify shell variables at the command line. Variables are values that have been associated in memory with some kind of identifier. Variables have many uses in Linux. To give a simple example, a variable with the identifier PATH contains the list of directories within which the shell can search for executable files. To view this list, you invoke the following command:
If you entered the command echo PATH, it would print the literal string “PATH”, rather than the value of the PATH variable, on your terminal.
The ~ character (tilde) enables you to refer quickly and easily to your home directory, no matter where in the file system you might be. Say that your current working directory is /usr/local/bin and you have a file called “usernames” in your own home directory, which you want to edit with vi. Rather than having to type the complete path to the file, you can just issue the command
Quoting special characters
There are occasions when you might want to suppress the special meanings of metacharacters. A special character must be quoted in order to represent its own literal meaning rather than its special meaning to the shell. Quoting causes the shell to overlook the unique capabilities of special characters and negate parameter expansion. This mechanism uses the following quote symbols:
- \ (backslash)
- ” (double quotes)
- ‘ (single quotes)
- ` (backquote)
The backslash is also known as the bash shell escape character. This is because it turns off or “escapes” the special meaning of the character that follows it. For example, as we have already seen, the following command returns the value of the PATH environment variable:
However, the following command will return the literal output “$PATH”, because the backslash negates the special meaning of the dollar sign:
If a line itself ends with a backslash, it acts as a continuation character for the line and the newline character is ignored.
Single quotes negate the translation of all special characters. They prevent the substitution of alternative values for the characters. For example, the following command will output a list of single-character filenames:
However, this command will attempt to list the file with the literal name “?”:
Double quotes cause most metacharacter special meanings to be ignored. The exceptions to this rule are the dollar sign, backquote, and backslash. So double quotes have the effect of canceling the process of filename generation by the shell, but still allow the expansion of shell variables and command substitution. The dollar sign and backquote continue to function as special characters when included between double quotes. The backslash character only does so when it’s followed by a dollar sign, backquote, double quote, backslash, or a newline character. In these circumstances, the backslash itself is removed and the special meaning of the following character is ignored. This makes it possible to quote a double quote between double quotes if it is preceded by a backslash. For example, the commands echo”\”” or echo\” output a double quote character by suppressing the special meaning of the embedded double quote.
The backquote is still often used for command substitution, although the $() combination (a dollar sign and brackets) is generally preferred. The shell interprets the text between a pair of backquotes as a command before translating the rest of the command line. The output of the command replaces the original backquoted text.
In the following code, the double quotes disable the date command:
> echo “date”
However, the backquotes in this command enable command substitution to occur:
Thu Jun 10 17:18:56 IST 2004