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 ~
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 ~ / работа / архив
, 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
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:
./script.sh
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.
.. 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.
компакт диск ..
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
/Path Directory Separator
You can use a forward slash (/)—often called a forward slash—to separate directories in a pathname.
ls ~ / work / archive
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:
компакт диск /
# 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}
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
? 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.
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
* Wildcard character sequence
You can use the asterisk wildcard to indicate any sequences characters, including lack of characters .
значок *
Consider the following filename pattern:
«ls badge *» command in a terminal window.
It matches «badge.txt» because the wildcard is any sequence of characters, or contains no characters.