15 Special Characters You Need to Know for Bash
If you want to master the Bash shell on Linux, macOS, or another UNIX-like system, special characters (like ~, *, |, and >) are critical. We'll help you unravel these cryptic Linux command sequences and become a hero of hieroglyphics.
What Are Special Characters?
There are a set of characters the Bash shell treats in two different ways. When you type them at the shell, they act as instructions or commands and tell the shell to perform a certain function. Think of them as single-character commands.
Sometimes, you just want to print a character and don't need it to act as a magic symbol. There's a way you can use a character to represent itself rather than its special function.
We'll show you which characters are "special" or "meta-" characters, as well as how you can use them functionally and literally.
~ Home Directory
The tilde (~) is shorthand for your home directory. It means you don't have to type the full path to your home directory in commands. Wherever you are in the filesystem, you can use this command to go to your home directory:
You can also use this command with relative paths. For example, if you're somewhere in the file system that's not under your home folder and want to change to thearchivedirectory in yourworkdirectory, use the tilde to do it:
. Current Directory
A period (.) represents the current directory. You see it in directory listings if you use the-a(all) option withls.
You can also use the period in commands to represent the path to your current directory. For example, if you want to run a script from the current directory, you would call it like this:
This tells Bash to look in the current directory for thescript.shfile. This way, it won't search the directories in your path for matching executable or script.
.. Parent Directory
The double period or "double dot" (..) represents the parent directory of your current one. You can use this to move up one level in the directory tree.
You can also use this command with relative paths—for example, if you want to go up one level in the directory tree, and then enter another directory at that level.
You can also use this technique to move quickly to a directory at the same level in the directory tree as your current one. You hop up one level, and then back down one into a different directory.
/ Path Directory Separator
You can use a forward-slash (/)—often just called a slash—to separate the directories in a pathname.
One forward-slash represents the shortest possible directory path. Because everything in the Linux directory tree starts at the root directory, you can use this command to move to the root directory quickly:
# Comment or Trim Strings
Most often, you use the hash or number sign (#) to tell the shell what follows is a comment, and it should not act on it. You can use it in shell scripts and—less usefully—on the command line.
It isn't truly ignored, however, because it's added to your command history.
You can also use the hash to trim a string variable and remove some text from the beginning. This command creates a string variable calledthis_string.
In this example, we assign the text "Dave Geek!" to the variable.
This command usesechoto print the words "How-To" to the terminal window. It retrieves the value stored in the string variable via a parameter expansion . Because we append the hash and the text "Dave," it trims off that portion of the string before it's passed toecho.
This doesn't change the value stored in the string variable; it only affects what's sent toecho. We can useechoto print the value of the string variable once more and check this:
? Single Character Bash Wildcard
Bash shell supports three wildcards, one of which is the question mark (?). You use wildcards to replace characters in filename templates. A filename that contains a wildcard forms a template that matches a range of filenames, rather than just one.
The question mark wildcard represents exactly one character. Consider the following filename template:
This translates as "list any file with a name that starts with 'badge' and is followed by any single character before the filename extension."
It matches the following files. Note that some have numbers and some have letters after the "badge" portion of the filename. The question mark wildcard will match both letters and numbers.
That filename template doesn't match "badge.txt," though, because the filename doesn't have a single character between "badge" and the file extension. The question mark wildcard must match a corresponding character in the filename.
You can also use the question mark to find all files with a specific number of characters in the filenames. This lists all text files that contain exactly five characters in the filename:
* Character Sequence Bash Wildcard
You can use the asterisk (*) wildcard to stand for any sequence of characters, including no characters. Consider the following filename template:
This matches all of the following:
It matches "badge.txt" because the wildcard represents any sequence of characters or no characters.
This command matches all files called "source," regardless of the file extension.
[] Character Set Wildcard
As covered above, you use the question mark to represent any single character and the asterisk to represent any sequence of characters (including no characters).
You can form a wildcard with the square brackets ( [] ) and the characters they contain. The relevant character in the filename must then match at least one of the characters in the wildcard character set.
In this example, the command translates to: "any file with a ".png" extension, a filename beginning with "pipes_0," and in which the next character is either 2, 4, or 6."
You can use more than one set of brackets per filename template:
You can also include ranges in the character set. The following command selects files with the numbers 21 to 25, and 31 to 35 in the filename.
; Shell Command Separator
You can type as many commands as you like on the command line, as long as you separate each of them with a semicolon (;). We'll do this in the following example:
Note that the second command runs even if the first fails, the third runs even if the second fails, and so on.
If you want to stop the sequence of execution if one command fails, use a double ampersand (&&) instead of a semicolon:
