15 Special Characters You Need to Know for Bash

A Linux terminal filled with text on a laptop.

If you want master the Bash shell On Linux, macOS, or another UNIX-like system, special characters (such as ~, *, |, and>) are essential. We will help you understand these encrypted command sequences and become a hieroglyphic hero.

What are the special characters?

There is a set of characters the Shell bash deals with two different ways. When you type them on the shell, they act as instructions or commands and prompt the shell to perform a certain function. Think of them as one-off orders.

Sometimes you just want to print a character and you do not need it to act as a magic symbol. You can use a character to represent himself rather than for his special function.

We'll show you which characters are "special" or "meta" characters, and how you can use them in a functional and literal way.

~ Basic Directory

The tilde (~) is a shortcut for your home directory. This means that you do not need to type the full path of your home directory in the commands. Wherever you are in the file system, you can use this command to access your home directory:

cd ~

You can also use this command with relative paths. For example, if you are in the file system that is not in your home directory and want to change the archive directory of your working directory, use the tilde to do so:

cd ~ / work / archive

. Current directory

A period (.) Represents the current directory. You see it in the directory listings if you use the -a (all) option with ls.

ls -a

You can also use the point commands in to represent the path to your current directory. For example, if you want to run a script from the current directory, call it as follows:


This tells Bash to search the current directory for the script.sh file. This way, it will not search the directories in your path for the executable or script match.

.. Parent folder

The double point or "double point" (..) represents the parent directory of your current directory. You can use it to go up one level in the tree.

cd ..

You can also use this command with relative access paths, for example, if you want to go up one level in the tree, and then enter another directory at that level.

You can also use this technique to quickly access a directory at the same level as your current directory. You go up one level, then go down to another directory.

cd ../gc_help

/ Path directory separator

You can use a slash (/) – often simply called a slash – to separate directories from an access path.

ls ~ / work / archive

A slash represents the shortest directory path possible. Since everything in the Linux directory tree starts at the root directory, you can use this command to quickly access the root directory:

cd /

# Comment or cut the chains

Most often, you use the pound sign or a number (#) to tell the shell that what follows is a comment and that it should not act on it. You can use it in shell scripts and, what is less useful, in the command line.

# This will be ignored by the Bash shell

However, it is not really ignored because it is added to the history of your orders.

You can also use the hash to cut a string variable and remove text from the beginning. This command creates a string variable called this_string.

In this example, we assign the text "Dave Geek!" To the variable.

this_string = "Dave Geek!"

This command uses echo to print the words "How-To" in the terminal window. It retrieves the value stored in the string variable via a expansion of parameters. Because we add the hash and the "Dave" text, it eliminates that part of the chain before it goes to echo.

echo How-To $ {this_string # Dave}

This does not change the value stored in the string variable; this only affects what is echoed. We can use echo to print the value of the string variable one more time and check this:

echo $ this_string

? Generic character with a unique character

The Bash shell supports three wildcard characters, one of which is the question mark (?). You use wildcard characters to replace the characters in the file name templates. A file name that contains a wildcard is a template that corresponds to a range of file names rather than just one.

The wildcard character of question mark represents exactly one character. Consider the following file name pattern:

ls badge? .txt

This translates to "the list of all files whose names begin with" badge "and which are followed by any characters before the file name extension."

This corresponds to the following files. Note that some have numbers and some letters after the "badge" part of the file name. The wildcard character of question mark will correspond to both letters and numbers.

However, this file name template does not match "badge.txt" because the file name does not contain a single character between "badge" and the file extension. The wildcard character of the query must match a corresponding character in the file name.

You can also use the polling point to search for all files that contain a specific number of characters in filenames. This lists all text files that contain exactly five characters in the file name:

ls ?????. txt

* Character sequence character

You can use the wildcard character asterisk (*) to designate any sequence of characters, including any characters. Consider the following file name pattern:

ls badge *

This corresponds to all the following elements:

It corresponds to "badge.txt" because the wildcard represents any sequence of characters or no characters.

This command corresponds to all the files called "source", whatever their extension.

the source. *

[] Generic character

As noted above, you use the question mark to represent a single character and the asterisk for 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 file name must then match at least one of the characters in the wildcard set.

In this example, the command translates to: "any file with the extension" .png ", a file name starting with" pipes_0 "and in which the next character is either 2, 4, or 6."

ls badge_0[246].sms

You can use more than one set of square brackets per file name template:

ls badge_[01][789].sms

You can also include ranges in the character set. The following command selects files with numbers 21 to 25 and 31 to 35 in the file name.

ls badge_[23][1-5].sms

; Shell command separator

You can type as many commands as you want on the command line, as long as you separate them with a semicolon (;). We will do this in the following example:

ls> count.txt; wc -l count.txt; rm count.txt

count.txt; wc -l count.txt; rm count.txt "command in a terminal window" width = "646" height = "122" src = "/ pagespeed_static / 1.JiBnMqyl6S.gif" onload = "pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon (this);" onerror = "this.onerror = null; pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon (this);" />

Note that the second command is executed even if the first fails, the third one is executed even if the second fails, and so on.

If you want to stop the run sequence in case of a command failure, use a double ampersand (&&) instead of a semicolon:

cd ./doesntexist && cp ~ / Documents / reports / *.

And process of background

Once you have typed a command into a terminal window and it is finished, you return to the command prompt. Normally, it only takes a few moments. But if you launch another application, such as gedit, you can not use your terminal window until you close the application.

However, you can launch an application as a background process and continue to use the terminal window. To do this, simply add an ampersand to the command line:

gedit command_address.page &

Bash shows you the process ID of what was started, and then takes you back to the command line. You can then continue to use your terminal window.

<Input redirection

Many Linux commands accept a file as a parameter and take their data from that file. Most of these commands can also take an input from a stream. To create a feed, use the left-angle (<) hook, as shown in the following example, to redirect a file into a command:

sort <words.txt

<img class = "alignnone wp-image-439313", data-pagespeed-lazy-src = "https://www.howtogeek.com/wp-content/uploads/2019/09/23.png.pagespeed. -hLuplhDWu.png "alt =" A "so

When a command has a redirected entry, it may behave differently than it reads from a named file.

If we use wc to count the words, lines and characters of a file, the values ​​are printed and then the file name. If we redirect the contents of the file to wc, it will display the same numeric values ​​without knowing the name of the file from which the data came from. It can not print a file name.

Here are some examples of wc use:

wc words.txt
wc <words.txt

> Output redirection

You can use the right-angle bracket (>) to redirect the result of a command (usually in a file); here is an example:

ls> files.txt
cat files.txt

files.txt "command in a terminal window." width = "646" height = "382" src = "/ pagespeed_static / 1.JiBnMqyl6S.gif" onload = "pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon (this);" onerror = "this.onerror = null; pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon (this);" />

Output redirection can also redirect error messages if you use a number (2, in our example) with>. Here's how to do it:

wc doesntexist.txt 2> errors.txt
chat.txt errors

errors.txt "command in a terminal window." width = "646" height = "122" src = "/ pagespeed_static / 1.JiBnMqyl6S.gif" onload = "pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon (this);" onerror = "this.onerror = null; pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon (this);" />

RELATED, RELATED, RELATED: What are stdin, stdout and stderr under Linux?

| Pipe

A "pipe" chained commands. It takes the output of one command and forwards it to the next one as input. The number of transmitted commands (the length of the string) is arbitrary.

In this case, we will use cat to feed the contents of the words.txt file into grep, which will retrieve any line containing an uppercase or uppercase "C", and then pass these lines to sort. sorting uses the -r (inverse) option, so sorted results will appear in the reverse order.

We typed the following:

words chat.txt | grep [cC] | sort -r

! NOT logical pipeline and operator history

The exclamation point (!) Is a logical operator that means NO.

There are two commands in this command line:

[ ! -d ./backup ] && mkdir ./backup
The first command is the text in square brackets;
The second command is the text following the double ampersands &&.

The first command uses! as a logical operator. The brackets indicate that a test will be done. The -d (directory) option tests the presence of a directory called backup. The second command creates the directory.

Since double ampersands separate the two commands, Bash will only execute the second if the first one succeeds. This is the opposite of what we need. If the backup directory test succeeds, you do not need to create it. And if the test directory "backup" fails, the second command will not run and the missing directory will not be created.

It is here that the logical operator! enter. It acts as a logical no. So, if the test succeeds (that is, the directory exists), the symbol! return this to "NOT successful", which is a failure. The second command is not activated.

If the directory test fails (that is, the directory does not exist), the symbol! change the answer to "NOT fail", which is a success. Thus, the command to create the missing directory is executed.

This little! Pack lots of punch when you need it!

To check the status of the backup folder, use the ls command and the -l (long list) and -d (directory) options, as shown below:

backup ls -l -d

You can also run commands from your command history with the exclamation point. The history command lists the history of your orders and you then type the number of the order with which you want to restart the execution! to run it, as shown below:

! 24

The following re-executes the previous command:


$ Variable expressions

In the Bash shell, you create variables to hold values. Some, like Environment variables, still exists and you can access it whenever you open a terminal window. These contain values, such as your user name, your home directory, and your path.

You can use echo to see the value a variable holds – just put the name of the variable with the dollar sign ($), as shown below:

echo $ USER
echo $ HOME
echo $ PATH

To create a variable, you must give it a name and assign it a value. You do not have to use the dollar sign to create a variable. You add only $ when you reference a variable, as in the following example:

ThisDistro = Ubuntu
MyNumber = 2001
echo $ ThisDistro
echo $ MyNumber

Add braces ({}) around the dollar sign and expand the parameters to get the value of the variable and to allow other transformations of the value.

This creates a variable that contains a string, as shown below:

MyString = 123456qwerty

Use the following command to return the string in the terminal window:

echo $ {MyString}

To return the substring from position 6 of the entire string, use the following command (there is a zero offset, so the first position is zero):

echo $ {myString: 6}

If you want to echo a substring that starts at the zero position and contains the following six characters, use the following command:

echo $ {myString: 0: 6}

Use the following command to echo a substring beginning at position four and containing the following four characters:

echo $ {myString: 4: 4}

Quote special characters

If you want to use a special character as a literal (non-special) character, you must inform the Bash shell. That's what we call, and there are three ways to do it.

If you put the text in quotation marks ("…"), it prevents Bash from acting on most special characters, which only print. The dollar sign ($) is, however, a notable exception. It always serves as a character for variable expressions, so you can include variable values ​​in your output.

For example, this command prints the date and time:

echo "Today is $ (date)"

If you put the text in single quotation marks ("…") as shown below, the function of all special characters is stopped:

echo today is $ (date) & # 39;

You can use a backslash () to prevent the next character from functioning as a special character. This is called "escape" to the character; see the example below:

echo "Today is $ (date)"

Just think of special characters as very short commands. If you memorize their uses, your understanding of the Bash shell and other people's scripts will be greatly enriched.

RELATED, RELATED, RELATED: 37 Important Linux commands to know

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.