If you want to master the Bash shell on Linux, macOS, or another UNIX-like system, special characters (such as ~, *, |, and >) are critical. We will help you unravel these cryptic Linux command sequences and become a hieroglyph hero.

What are special characters?

There is a set of characters that the Bash shell handles in two different ways. When you type them into the shell, they act like instructions or commands and tell the shell to perform a specific function. Think of them as single character commands.

Sometimes you just need to type a character and you don’t need it to act like a magic character. There is a way that you can use a symbol to represent itself rather than its special function.

We’ll show you which characters are «special» or «meta», as well as how you can use them functionally and literally.

home directory

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

  CD ~ 

Command "cd ~" in the terminal window.

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

  CD ~ / работа / архив 

"cd ~/work/archive" command in a terminal window.

, Current directory

The dot (.) represents the current directory. You will see this in directory listings if you use the option -a (all) with ls .

  ls -a 

"ls -a" command in a terminal window.

You can also use a dot in commands to represent the path to your current directory. For example, if you wanted to run a script from the current directory, you would call it like this:


This tells Bash to look in the current directory for a file script.sh . This way it won’t look for directories in your path to find an executable or script.

"./script.sh" command in terminal window.

.. 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 a directory tree.

  компакт диск .. 

Command "cd .." in a terminal window.

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

You can also use this technique to quickly navigate to a directory at the same level in the directory tree as the current one. You jump up one level and then back to another directory.

  cd ../gc_help 

"cd ../gc_help" command in a terminal window.

/Path Directory Separator

You can use a forward slash (/)—often called a forward slash—to separate directories in a pathname.

  ls ~ / work / archive 

"ls ~/work/archive" command in a terminal window.

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

  компакт диск / 

"cd /" command in a terminal window.

# Comment or trim lines

Most often, you use a hash or number sign (#) to tell the shell that a comment follows and shouldn’t affect it. You can use it in shell scripts and — less usefully — on the command line.

  # Это будет игнорироваться оболочкой Bash 


However, it is not ignored because it is added to the command history.

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

In this example, we assign the text «Dave Geek!» variable.

  this_string = "Дейв Гик!" 


This command uses echo to display the word «How-To» in the terminal window. It retrieves the value stored in the string variable via parameter expansion. Since we’re adding the hash and the text «Dave», it trims that part of the string before passing it to echo .

  echo How-To $ {this_string # Dave} 

Command "echo How-To ${this_string#Dave}" in a terminal window.

This does not change the value stored in the string variable; it only affects what is sent to echo . We can use echo to print the value of the string variable again and test it:

  echo $ this_string 

"echo $this_string" command in terminal window.

? Single character wildcard

The Bash shell supports three wildcard characters, one of which is the question mark (?). You use wildcards to replace characters in filename patterns. A filename that contains a wildcard forms a pattern that matches a range of filenames rather than just one.

The wildcard question mark represents exactly one character . Consider the following filename pattern:

  ls badge? .txt 

This translates to «list any file with a name beginning with ‘badge’ and 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 icon portion of the filename. The wildcard question mark will match both letters and numbers.

"ls badge? .txt" command in terminal window.

This filename pattern does not match the value «badge.txt» because there is not a single character in the filename between «icon» and the file extension. The wildcard question mark must match the corresponding character in the file name.

You can also use the question mark to find all files with a certain number of characters in their filenames. This lists all text files that contain exactly five characters in their filename:

  ls ?????. txt 

"ls ?????.txt" command in terminal window.

* Wildcard character sequence

You can use the asterisk wildcard to indicate any sequences characters, including lack of characters .

  значок * 

Consider the following filename pattern:

This matches all of the following:

«ls badge *» command in a terminal window.

It matches «badge.txt» because the wildcard is any sequence of characters, or contains no characters.

  Источник. * 

This command matches all files referred to as "source" regardless of the file extension.

[] Command «ls source. *» In a terminal window.

Character set

As stated above, you use a question mark for any single character, and an asterisk for any sequence of characters (including characters).[]You can form a wildcard with square brackets ( ) and the characters they contain.

The matching character in the filename must match at least one of the characters in the wildcard character set.

  ls badge_0 [246] .txt 

This example converts the command to "any file with a

.txt» in a terminal window.

  ls badge_ [01] [789] .txt 

You can use more than one set of parentheses per filename pattern: [01] [789]  Command "ls badge_

.txt» in a terminal window. You can also include ranges in the character set.

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

The following command selects files with numbers 21 to 25 and 31 to 35 in the filename. [23] [1-5]  Command "ls badge_

.txt» in a terminal window. ;

Shell command separator On the command line, you can enter as many commands as you like, as long as you separate each one with a semicolon (;).

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

We will do this in the following example: count.txt; wc -l count.txt; rm count.txt» in terminal window

The command «cd ./doesntexist && cp ~ / Documents / reports / *.» In the terminal window.

& background process After you type the command in the terminal window and it completes, you will be returned to the command prompt. It usually only takes a couple of seconds. gedit But if you run another application like

you won’t be able to use the terminal window until you close the application. However, you can run the application as a background process and continue using the terminal window.

  gedit command_address.page & 

To do this, simply add an ampersand to the command line:

The command «gedit command_address.page &» in a terminal window. Bash shows the process ID of the process that was started and then returns you to the command line.

You can then continue using the terminal window.

Many Linux commands take a file as a parameter and take their data from that file. Most of these commands can also receive data from a stream.


To create a stream, use the left angle bracket (<) as shown in the following example to redirect a file to a command:'<words.txt"

sort command

When a command redirects input to it, it may behave differently than when reading from a named file. wc If we use to count the words, lines, and characters in a file, it prints the values ​​followed by the file name. wc If we redirect the contents of the file to it will print the same numeric values, but will not know the name of the file from which the data came.

It cannot print the filename. wc Here are some examples of how you can use

  wc words.txt 


Command «wc words.txt» in the terminal window.

> Output Redirection You can use the right angle bracket (>) to redirect the output of a command (usually to a file);

  ls> files.txt 
  cat files.txt 

here is an example: files.txt» in terminal window.

«!24» command in terminal window.


The following re-runs the previous command:

$Variable Expressions In the Bash shell, you create variables to store values. Some, such as environment variables, are always there and you can access them any time you open a terminal window.

They contain values ​​such as your username, home directory, and path. echo you can use

  echo $ USER 
  echo $ HOME 
  echo $ PATH 

to see the value that a variable contains, simply precede the variable name with a dollar sign ($), as shown below:

«echo $USER» command in a terminal window. To create a variable, you must give it a name and provide a value to store it. To you not you need to use the dollar sign to create a variable. $ You are only adding

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

when referring to a variable, for example in the following example:

Command «ThisDistro=Ubuntu» in a terminal window.

Add curly braces ({}) around the dollar sign and expand the parameter to get the value of the variable and allow further value conversions.

  MyString = 123456qwerty 

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

  echo $ {MyString} 

Use the following command to print a string to a terminal window:

  echo $ {myString: 6} 

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

  echo $ {myString: 0: 6} 

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

  echo $ {myString: 4: 4} 

Use the following command to display the substring that starts at the fourth position and contains the following four characters:

The command «MyString = 123456qwerty» in the terminal window.

Quoting special characters If you want to use a special character as a literal (non-special) character, you must specify the Bash shell.

This is called quoting, and there are three ways to do it. Enclosing text in quotation marks («…») will prevent Bash from affecting most special characters, and they will just print. However, one notable exception is the dollar sign ($).

It still acts as a symbol for variable expressions, so you can include values ​​from variables in your output.

  echo "Today is $ (date)" 

For example, this command prints the date and time: If you enclose the text in single quotes (‘…’) like below, it stops the function all

  echo 'Today is $ (date)' 

special characters: You can use a backslash (\) to avoid using the next character as a special character. This is called «avoiding» the character;

  echo "Today is \ $ (date)" 

see example below:

Team Just think of special characters as very short commands.

If you memorize their usage, it can greatly benefit your understanding of the Bash shell and other people’s scripts. RELATED:

Похожие записи