Folder Path Separator (/)

In Bash, the forward-slash (/)separates the parts of a path, the subfolders-within-folders. To visit the folder named “pictures” inside your home folder, you’ll have to use the command cd as: Everything after a forward slash in the above example resides within what precedes the slash.

Home Directory (~)

Instead of typing the full name of your home folder in your Bash terminal, you can use the tilde character (~). For example, to go to your home folder, use: You can also incorporate it into more complex paths. For example, to edit a file named “mydata.txt” inside the “Personal” folder in your Home directory, use:

Current / Above Folder (.)

You can use a single (.) or double dot (..)to define whether an action should be performed inside the current directory or the one above, respectively. A single dot (.) maps to the current folder while a double dot (..) maps to the folder above it. Let’s say you’re in the folder “/home/USERNAME/pictures” and want to execute the script called “transform_images.sh” within the same directory. In this case, type: If, after executing the script, you want to return to the folder above the one you’re currently in, type: That would return you from the folder “/home/USERNAME/pictures” to “/home/USERNAME.”

Comments and Cancels (#)

The hash symbol (#) is more useful when writing Bash scripts since it allows you to add comments to them for future reference. Bash ignores everything following a hash symbol. In the following script, the first line defines it’s a Bash script, the second is a comment that’s ignored, and the third is a typical copy command: Hashes are useful even if you’re not writing a script since they allow you to cancel parts of a command. To see that in action, try the following straightforward command: Then, try the following instead: You’ll only see “I am” returned in the second version because the hash will have canceled everything that followed after.

Ranges ([])

You can define character ranges by enclosing them in brackets ([]). To see that in action, let’s say you want to look for folder names that start with either D or M. Type: Perhaps you’re in a folder filled with subfolders named after each year instead. To copy the folders for the previous five years into /home/USERNAME/backup, use: You can even simplify them further with a dash (-): Bash will iterate from 5 to 9 to include the numbers between them.

Redirection (<>)

Using angle brackets (<>), you can redirect a command’s input or output. For example, the following command: will redirect the output of ls and save it to the “list.txt” file. Note that a double right-angle bracket (») appends a command’s output to a file. If you re-run the same command, it will append its output to the end of the existing content. To replace its content with new results, use a single right-angle brackets (>):

Pipes (|)

You can combine different commands into a larger whole to achieve more complex results by using pipes (|). They’re somewhat similar to redirection (more on their similarities and differences here). Suppose you have a huge file with thousands of entries and want to locate your name in it. Instead of searching for it in a text editor, do the following: In this case, the output of “entries.txt” will be piped to the grep command.

Command Separator (;)

Bash allows you to issue multiple commands in one go by separating them with semicolons (;). For example, to copy two folders to two different destinations with one command: The semicolon separates the two commands and tells Bash to execute them sequentially. Note that you can use more than two commands if you wish.

Wildcards (*)

You’ve probably already used the asterisk (*) in some commands. It matches any sequence of characters and allows actions like copying all JPG files from one folder to another: The question mark (?) is also a wildcard in Bash but only matches a single character. For example: The above command will copy all jpg files in folders that begin with “201.” Since the wildcard translates to any alphanumeric character, not only numbers, the above command would also copy any folder that might be named “201A” or “201z.”

Launch in Background (&)

You can run commands as background processes just by appending the command with an ampersand symbol (&): The above will start copying the file huge_file.zip and immediately move to the background, letting you keep using the terminal. The command will automatically exit when it completes. If you want to bring it forward again, you can do so by typing fg followed by Enter.

Variables ($)

The dollar sign ($) allows you to set up variables for use in your commands. To see them in action, try entering the following in your terminal: Note that there’s no dollar sign when assigning values to variables.

Escapes () and Quotes (”)

If you want to use any of the special characters as it is in a command, you’ll have to escape it. You can do that by preceding the special character with a backslash (). For example, if you have a file with a name that includes an exclamation mark, you’d have to type ! instead for Bash to interpret it as an exclamation mark and not as a special character. Another way would be using either single (’’) or double-quotes (""). By enclosing a string in quotes, any special character in it will be treated as the actual character. There is a difference between single and double quotes, too. Single quotes will evaluate the enclosed string as text while double quotes allow you to use variables ($) within the enclosed string.

Wrapping Up

The above aren’t the only special characters in Bash, but they’re the ones we consider most useful for our daily adventures in the terminal. Don’t forget to check out these keyboard shortcuts to quickly move around in Bash. Do let us know in the comments section below if we have missed any important special character.