A complete list of the meanings of special symbols in Linux terminals

Linux special symbols usage and meaning

Summarize

There are many special symbols in the Linux terminal. This article summarizes the commonly used ones:

  1. $Represents a variable/ordinary terminal user: Used to reference the value of a variable/represents an ordinary user in the terminal.
  2. #Represents comments/superuser: Used to comment code in scripts/represents superuser in terminal.
  3. /:Root directory/directory separator.
  4. ~: The current user’s home directory.
  5. .:Current directory.
  6. ..:Parent directory.
  7. <Enter redirection symbol: Enter the contents of the specified file into the command.
  8. >Output redirection symbol: Redirect command output to the specified file.
  9. >>Output append symbol: used to append the output of a command to the end of the file.
  10. <<Enter the append symbol: generally used to enter multi-line text.
  11. ;Command delimiter: Multiple commands can be executed on one line.
  12. :Value/path separator: splits a label and a value, or splits a path, which means no action in a bash script.
  13. &Background run symbol: Put the command to run in the background.
  14. |Pipe symbol: Use the output of one command as the input of another command.
  15. &&Multiple command notation: used to execute multiple commands on the same line. It means that the next command will be executed only after the previous command has been executed successfully.
  16. ||Multi-command symbol: used to execute multiple commands on the same line. It means that the next command will be executed only if the previous command fails to execute.
  17. \Escape symbols: used to escape special characters.
  18. !Indicates historical commands: used to execute historical commands.
  19. ^Caret: represents a control character, used to quickly replace previous instructions or perform regular matching.
  20. ()Subcommand symbol: used to execute subcommands in the current shell.
  21. *Wildcard: represents any number of any characters.
  22. ?Wildcard: represents any character.
  23. []Character set: used to match any character in the specified character set.
  24. {}Curly brace notation: used to expand a string or sequence of commands.

Terminal special symbols

$general user

$Symbol is a prompt in the Linux terminal, which usually appears at the beginning of the command line. Its function is to prompt the user to enter commands. When you see $the symbol appear on the command line, it means that the system is ready to accept your input, and you can enter any valid command.

In addition, $it can also be used to indicate that the current user is an ordinary user rather than a super user (i.e. root user). If the current user is root, the command prompt will become #.

In addition to this, in Linux, $symbols are often used to represent the values ​​of environment variables. Environment variables are some system-level variables that can be accessed and used throughout the system, which store some important information, such as system path, current user, current working directory, etc. Using $the notation, we can easily access these variables.

Here are some examples:

  1. $HOMEVariable: It points to the current user's home directory. For example, if the current user is "bob", then this $HOMEwould be "/home/bob".
  2. $PATHVariable: It contains a series of directories used to find executable programs. When you enter a command in the terminal, the system looks for the executable file for the command in these directories. For example, if you want to run the "ls" command in the terminal, the system will $PATHlook for the "ls" executable in the directory listed in the variable.
  3. $USERVariable: It points to the currently logged in user. For example, if the current user is "bob", then this $USERwill be "bob".
  4. $PWDVariable: It points to the current working directory. For example, if you run the terminal in the "/home/bob" directory, then $PWDit will be "/home/bob".

You can use the "echo" command in the terminal to display the values ​​of these variables, for example:

echo $HOME
echo $PATH
echo $USER
echo $PWD

These commands will output the current user's home directory, the system's executable path, the currently logged in user name, and the current working directory respectively.

image-20230520225155502

printenvYou can view all environment variables through , printenvwhich is a Linux command used to print out all environment variables and their values ​​​​in the current shell environment. Its use is very simple, just enter in the terminal printenv:

image-20230520225429504

#root

In Linux systems, $ the symbol usually represents the command line interface of an ordinary user, while #the symbol represents the command line interface of a superuser. Ordinary users usually only have the authority to execute commands, while super users have higher authority and can perform higher-level operations, such as modifying system configuration files. Generally, they need to use the sudo or su command to switch to root authority before they can use it.

~Current user's home directory

In the Linux terminal, ~the (tilde) symbol represents the current user's home directory. The home directory is the default working directory of each user in the Linux system. When a user logs in to the system, the terminal will automatically switch to the user's home directory.

For example, if the currently logged in user is "user", then ~the symbol represents the "/home/user" directory. Enter the command in the terminal cd ~to switch to the current user's home directory.

In addition, ~the symbol can also be used in combination with other paths to represent a path relative to the current user's home directory. For example, ~/Documentsit represents the Documents subdirectory under the current user's home directory.

/Directory separator

In Linux, the symbol "/" represents the file path separator. In the file system, each file and directory has a unique file path. The file path is a string consisting of a series of directory and file names separated by the "/" symbol.

For example, “/home/user/Documents/file.txt”it is a file path, where "/" represents the root directory, "home" is a subdirectory under the root directory, "user" is a subdirectory under the "home" directory, and "Documents" is under the "user" directory. subdirectory, "file.txt" is a file in the "Documents" directory.

In the command line, users can use "/" to specify the file path, for example:

cd /home/user/Documents

This will take you to the "Documents" directory.

.Current directory

.: Indicates the current directory, for example, ./file.txtindicates a file in the current directory file.txt. , you can also use $PWDto represent

..Parent directory

..: Indicates the upper-level directory, for example, ../means returning to the upper-level directory.

<input redirect

<: Use the contents of a file as input to the command. For example, sort < file.txt uses the contents of the file.txt file as input to the sort command.

>Output redirection

>: Indicates the redirection character, used to redirect the output of the command to a file, which will overwrite the original content. For example, ls > file.txt writes the output of the ls command to the file.txt file. If the file does not exist, it will be created. If it exists, the original content will be overwritten.

There are many other uses for output redirection:

  • 2>: Redirect the command's error output to a file. For example, ls /invalid_dir 2> error.txt writes the error information of the ls command into the error.txt file.
  • &>: Redirect both the command output and error output to a file. For example, ls /invalid_dir &> output.txt writes the output and error information of the ls command to the output.txt file.

<<Enter append redirect

<<It is an input redirection symbol in Linux, also known as "Here Document", generally used to enter multi-line text. Its function is to use a piece of input in the shell as the input of a command or script, and use a specific identifier (can be any string) to indicate the end of the input. Specifically, it can pass a block of text as standard input to a command or script, rather than manually typing it line by line.

For example, suppose we want to write three lines of text to a file called output.txt. If we use the echo command to output line by line, we need to enter the command three times, as shown below:

echo "This is line 1" > output.txt
echo "This is line 2" >> output.txt
echo "This is line 3" >> output.txt

However, if we use << the notation, we can enter all three lines of text in one command, like this:

cat << EOF > output.txt
This is line 1
This is line 2
This is line 3
EOF

This method allows us to enter multiple lines of text faster and more conveniently, and is very useful for scenarios where a large amount of text needs to be entered.

image-20230520230719692

>>Output append redirection

>>: Indicates appending a redirection character, which is used to append the output of the command to the end of the file without overwriting the original content. For example, echo "hello" >> file.txt will append "hello" to the end of the file.txt file.

;command separator

;: Indicates the command separator. Multiple commands can be entered on the same line, separated by semicolons. When you enter multiple commands on the same line and separate them with semicolons, Linux executes the commands sequentially.

For example, the following command will create a testfolder named and a file.txttext file named inside it:

mkdir test; cd test; touch file.txt

This command will be interpreted as three separate commands: first create a testfolder named , then enter that folder, and finally create an empty file.txtfile named inside it.

It should be noted that semicolons do not support logical control of commands, such as if statements, loop structures, etc. If such control is required, use other command delimiters such as double vertical bars (||) or double ampersands (&&).

:value/path separator

In Linux, the colon ( :) can have the following functions:

  1. Label and value separator: In some configuration files or scripts, colon is used as a separator between labels and values.
    For example, /etc/passwdused to store user account information, each row consists of multiple fields separated by colons: username:password:uid:gid:home_directory:shell.
  2. Delimiter for environment variables: In environment variables, especially PATHvariables like , the colon is used as the path separator. It separates multiple paths and specifies the order in which executable programs are searched when executing a command.
    For example, PATHa variable can contain multiple paths, such as /usr/local/bin:/usr/bin:/bin. When a command is executed, the system looks for executable programs in these paths from left to right.
  3. No operation (placeholder): In some scripting and programming languages, the colon can be used as a no operation (no operation), indicating that no actual operation is performed, but only as a placeholder or no operation.
    For example, in a Bash script, :it can be used as truea replacement for the command to indicate that the condition is always true, such as:
if [ condition ]; then
  :
else
  command
fi

This can be used as a placeholder in some situations or to temporarily disable some code logic.

image-20230522150114975

&background runner

&: Indicates the background runner, which can beRun the command in the background. Specifically, when you enter a command in the terminal and add at the end &, the command will run in the background, that is, you can continue to enter other commands in the terminal without waiting for this command to complete.

For example, if you want to start a long-running process but don't want it to take up your terminal, you can put it in the background and run it like this:

my-long-running-process &

This way, the process will run in the background and you can continue to enter other commands in the same terminal. Note that if the command you are running in the background outputs any information, it will be output directly to the terminal and may interfere with other commands you are entering.

|pipe character

|: represents the pipe character, used to use the output of one command as the input of another command, thusImplement a combination of two or more commands. For example, if you want to list all files in a directory on a Linux system and sort them by file size, you can use the following command:

ls -l | sort -k 5 -n

Among them, ls -lthe command lists all files in the directory, while sort -k 5 -nthe command sorts by the fifth column (i.e. file size). By using the pipe symbol "|", these two commands can be combined to pass the output of the "ls" command to the input of the "sort" command. This way, you can accomplish two operations in one line of command.

&&(Success) then execute multiple commands

&&is a symbol in the Linux terminal used to execute multiple commands on the same line. It means that the next command will be executed only after the previous command has been executed successfully. For example, if you want to execute two commands at the same time in the terminal, such as updating packages and clearing useless packages, you can use the following command:

sudo apt-get update && sudo apt-get autoremove

This command will first update the package and thenThe automatic removal command will only be run after the update is successful. This ensures that all commands execute successfully without unexpected errors.

||(Failure) then execute multiple commands

In the Linux terminal, ||it is a logical operator, which means "or". It is used to connect two conditions. If one of the conditions is true, the entire expression is true. In the terminal, ||often used to build command line statements to ensure that the next command is executed only if the previous command fails. For example, if you want to run two commands in the terminal, but only run the second command if the first command fails, you can use the following command:

command1 || command2

If command1 is executed successfully, command2 will not be executed; if command1 fails, command2 will be executed.

!Execute historical commands

In a Linux terminal, !symbols are used to execute commands from the history of previously executed commands. Here are some examples:

  1. Repeat the previous command: Use !!the command to quickly repeat the previous command, for example:

    $ ls -l
    total 0
    $ !!
    total 0
    
  2. Repeatedly execute a command in the command history: Use !the command plus the command number to repeatedly execute any command in the command history, for example:

    $ history
      1  ls -l
      2  cd ..
      3  pwd
    $ !2
    cd ..
    

    In the above example, !2it means executing the second command in the history.

    image-20230520231309097

  3. Repeatedly execute a command starting with a certain string: Use !the command plus part of the command string to repeatedly execute a command starting with the string, for example:

    $ ls -l
    total 0
    $ !ls
    total 0
    

    In the above example, !lsit means executing the latest lscommand starting with , that is ls -l.

By using the "!" symbol, we can use the command line interface more efficiently, saving the time and effort of repeatedly entering the same command.

^caret

In the Linux terminal, ^the symbol is a special character that represents the "caret", which has multiple uses:

  1. Used to represent a control character: for example, ^Cthe combination of Ctrl+C keys, ^Dthe combination of Ctrl+D keys.

If you want to represent a control character in the Linux terminal, you can use ^the symbol plus the letter of the corresponding control character. For example, ^Cit represents the Ctrl+C control character, and ^Dit represents the Ctrl+D control character.

For example, if you want to enter the Ctrl+C control character in the terminal, you can press Ctrl+V, and then press Ctrl+C. At this time, the terminal will display a "^C" to indicate that the control character has been successfully entered.

  1. Used to quickly modify the command line: If you enter a command in the terminal, but find an error in it, you can use ^the symbol to quickly modify it. For example, if you enter the command "cd /user/local/bin", but the correct directory name is "usr", you can enter "^/user/usr" in the terminal and it will automatically change to "cd /usr/local/ bin".

Additionally, ^symbols can be used in commands to reference arguments from previously executed commands. Specifically, ^it is followed by a number n, which refers to the nth parameter in the previously executed command.

For example, suppose we previously executed the following command:

ls -l /usr/bin

Now, if we want to execute this command again, but replace “/usr/bin” with “/usr/sbin”, we can use the “^” symbol to refer to the parameters from the previously executed command. Specifically, we can type the following command:

^bin^sbin

" bin sbin" in this command means replacing "bin" in the previous command with "sbin". After executing this command, the terminal will automatically replace the previously executed command with a new command and execute the new command. The output result is as follows:

ls -l /usr/sbin

That's ^what symbols are for. It can help us quickly refer to parameters in previously executed commands, thereby avoiding repeatedly entering similar commands.

image-20230520231829537

  1. Used to match the beginning of a line: In regular expressions, ^the symbol indicates matching the beginning of a line. It can be used to quickly locate and manipulate text at the beginning of a line.

For example, suppose I want to find lines starting with the letter "t" in a file, then I can use the following command:

grep "^t" filename

This command uses ^the symbol to match the beginning of the line, followed by the character to be matched t. This grepwill look for lines starting with the letters in the file tand print them to the terminal.

\Escapes

\: represents the escape character, which canEscape special characters, giving it a special meaning. Here are some examples:

  1. The escape character itself: If you need to use the "\" character in the command line, you need to use "\" to represent it. For example, to create a folder called "test\file" you would use the following command:

    mkdir test\\file
    
  2. Newline character: If you want to break a newline in the command line, you can use "\n" to express it. For example, to print two lines of text on the command line, you would use the following command:

    echo "This is the first line.\nThis is the second line."
    
  3. Spaces: Sometimes you need to use spaces in file or folder names, but spaces are interpreted as parameter separators on the command line. In this case, you can use "\" to escape the spaces. For example, to open a folder named "my folder" you would use the following command:

    cd my\ folder/
    

Note: Be careful when using escape characters. If you accidentally put them in the wrong location, it may cause the command to not execute correctly.

"Extended quoted string

": Represents a quoted string, the contents of which can be passed to the command as a whole. In double quotes, you can use the escape character (\) to escape some special characters, such as $ and \, so that they lose their original special meaning and become ordinary characters.

For example, the following command will print Hello, World! in the terminal:

echo "Hello, World!"

Variables can also be used within double quotes, for example:

name="John"
echo "My name is $name"

This will print out in the terminal My name is John. It should be noted that when using variables within double quotes, the variable name must be preceded by a $ symbol.

image-20230520222420079

'Do not expand quoted strings

': also means quoting a string, but the difference between it and double quotes is that theAll characters are treated as ordinary characters without any escaping or variable substitution.. For example, if we want to define a string containing special characters, such as $, *, ?, etc., we can surround it with single quotes to avoid the special characters being interpreted as variables or wildcards:

echo 'Hello $USER, the current directory is: $PWD'

In the above example, single quotes surround the entire string, so $USERand $PWDare not interpreted as variables but are output as normal characters. The output is as follows:

Hello $USER, the current directory is: $PWD

()Create subshell

In the Linux terminal, the bracket notation ()is usually usedCreate subshell. This means that the command is run in a subshell, not the current shell. For example, if you want to create a folder called "test" in the current directory and switch to that folder, you can use the following command:

pwd && (cd /etc && pwd)

In this command, ()the symbols cd /etc && pwdput the command into a subshell. This means that cd /etcthe command will only be run in the subshell and will not affect the current shell's working directory. Therefore, pwdthe command will display etcthe full path of .

image-20230520223219986

As another example, if you want to set an environment variable in the current shell, but only want it to take effect in one command, you can use bracket notation to put the command into a subshell. For example:

(export MYVAR="Hello world"; echo $MYVAR)

This command will set an MYVARenvironment variable named , set its value to "Hello world", and print it to the terminal. Because the command is run in a subshell, MYVARthe variables only exist for the duration of the command execution .

*multi-character wildcard

In the Linux terminal, the asterisk *is the wildcard symbol, also known as the wildcard character. it can representany character or sequence of characters of any length. The asterisk is commonly used in filename expansion and searches, for example:

  • List all files ending with ".txt":

    ls *.txt
    
  • Delete all files ending with ".bak":

    rm *.bak
    
  • Find all files containing the text "hello":

    grep "hello" *
    

In these examples, the asterisk wildcard will match all matching file names and be expanded to the actual file name before executing the command.

?single character wildcard

In the Linux terminal, ?the symbol represents a wildcard character that can match any character. When you enter a command in the terminal, if you need to match a certain number of characters, but you are not sure what these characters are, you can use the ?symbol to replace these characters. For example, if you want to find files starting with "file",followed by an arbitrary character, and then the file ending with ".txt", you can use the following command:

ls file?.txt

This command will list all files matching this pattern.

[]condition matching

In a Linux terminal, input []is usually used to match any one character in a set of characters. This matching method is called a character class, which allows us to specify multiple characters in a character set and then match any one of them.

For example, assume we have the following files in the current directory:

file1.txt file2.txt file3.jpg file4.png

If we want to list all .txtfiles ending with , we can use the following command:

ls *[.]txt

In this command, [enclosed ]by ., means matching the period itself, not any character. In the character class, we can specify multiple characters, using -to represent the range. For example, if we want to match all lowercase letters and numbers, we can use [a-z0-9]. If we want to match all uppercase and lowercase letters, we can use [A-Za-z].

In short, []it is one of the very common usages in the Linux terminal and can be used to match file names, search text, etc.



If you have any questions or errors, please feel free to message me privately for corrections.
All rights reserved. Please do not reproduce without authorization!
Copyright © 2023.05 by Mr.Idleman. All rights reserved.


Guess you like

Origin blog.csdn.net/qq_42059060/article/details/130787026