Home > Hosting > Server

What are the special characters and their meanings in linux

2023-05-18 14:57:03

<p>This article mainly explains what are the special characters and their meanings in linux. What are the special characters and their meanings!</p><p><br/></p><blockquote><p>Linux&#39;s special characters: 1, # comment character; 2, ~, representing the user&#39;s home directory; 3, ;, is a symbol serving as a continuous command function; 4, /, path directory separator; 5, , escape character; 6, |, pipe character; 7, ., represent the current directory; 8, .., represent the parent directory, which is the upper level directory of the current directory ; 9, ?, refer to single-character wildcards, which represent the matching of any character in the file name; 10, *, represent any sequence of characters, match any character, etc.</p></blockquote><h3><strong>The use and meaning of linux special symbols</strong></h3><p><strong>#: </strong></p><p>Start with # , which means that this sentence is a comment.</p><p><br/></p><p><code>#! /bin/bash often appears before the command, or after the command, followed by a comment text, which will not be executed #When the entered command does not want to be executed, enter (#) on the line #This line is comments. (This is a line of comments)</code></p><p><br/></p><p>Although the above paragraph is ignored, it will still be added to your command history.</p><p>A more powerful approach is as follows:</p><p>First define a variable and assign it the string amazing alvin</p><p><br/></p><p><code>this_string=&quot;amazing alvin &quot;</code></p><p><br/></p><p><br/></p><p>${this_string#amazing} returns the this_string string variable with amazing commented out, you can echo the output to see the result:</p><p><br/></p><p><code>echo awsome ${this_string#amazing}</code></p><p><br/></p><p>amazing is just commented out, it is not deleted, remove the comment It returns:</p><p><br/></p><p><code>echo $this_string</code></p><p><br/></p><p><br/></p><p><strong>~</strong></p><p>~ represents the user&#39;s home directory, which refers to the home directory, which is our user&#39;s personal directory, no matter where you are, enter cd ~ it will take you backHome!</p><p><br/></p><p><code>cd ~</code></p><p><br/></p><p><br/></p><p>A more advanced way of playing is to add a specific path after it , directly locate the specified location in the home directory, isn&#39;t it very convenient?</p><p><br/></p><p><code>cd ~/work/archive</code></p><p><br/></p><p><br/></p><p><strong>; semicolon</strong></p><p>In the shell, the symbol that acts as a &quot;continuous command&quot; is the &quot;semicolon&quot;.</p><p>This is similar to the &quot;;&quot; we use every day, so I won&#39;t go into details, it is mainly used to separate commands.</p><p><br/></p><p><code>ls &gt; count.txt; wc -l count.txt; rm count.txt</code></p><p><br/></p><p><br/></p><p>&lt; Note here that when you separate commands with ;, even if the first command fails, the second command will run, even if the second command fails, the third command will run, and so on.</p><p>If you want to stop if a command fails, use &quot;&amp;&amp;&quot; as follows:</p><p><br/></p><p><code>cd ./doesntexist &amp;&amp; cp ~/Documents/ reports/* .</code></p><p><br/></p><p><br/></p><p><strong>;; Continuous semicolon (Terminator)</strong></p><p>is dedicated to The option of case, assumes the role of Terminator.</p><p><br/></p><p><code>case &quot;$fop&quot; inhelp) echo &quot;Usage: Command -help -version filename&quot;;;version) echo &quot;version 0.1&quot;;;esac</code></p><p><br/></p><p><strong>. dot</strong></p><p>In the shell, users should be aware that one <code>.</code> represents the current directory, two &lt; code&gt;.. represents the upper directory<br/></p><p>Let&#39;s take a look at all the files in the current directory:</p><p><br/></p><p><code>ls -al</code></p><p><br/></p><p><img src="//img.freeonlinedomain.com/uploads/allimg/20230518/1-23051Q45950P7.jpg" title="" alt="1.jpg"/></p><p>The . in the red circle refers to the current directory, but this is meaningless, we use it more in commands, as follows:</p><p><br/></p><p><code>./script.sh</code></p><p><br/></p><p>This is telling bashJust find and execute the script.sh file in the current directory, no need to find it in the path.</p><p><br/></p><p>Suppose we want to go back to the previous directory:</p><p><br/></p><p><code>cd ..</code></p><p><br/></p><p><br/></p><p>Same as before, you can add a specific directory after it, which means to locate other directories at the same level as the current directory:</p><p><br/></p><p><code>cd ../projects/</code></p><p><br/></p><p><br/></p><p><strong>&#39;string&#39; single quotes (string single quotes)</strong> </p><p>The content enclosed by single quotes will be treated as a single string. The $ symbol representing a variable within quotation marks has no effect, that is, it is treated as a general symbol, preventing any variable substitution.</p><p><br/></p><p><code>hello world=hello world echo &#39;$hello world&#39; # We get $hello world</code></p><p><br/></p><p><strong>string double Quotes (string double quotes)</strong></p><p>The content enclosed by double quotes will be treated as a single string. It prevents wildcard expansion, but allows variable expansion. This is different from the way single quotes are handled</p><p><br/></p><p><code>heyyou=homeecho &quot;$heyyou&quot; # We get home</code></p><p><br/></p><p><strong><code>command (command)</code> Backquote (backquote)</strong></p><p>The single and double quotes in front enclose a string, but if the string is a column Command line, what happens? The answer is that it will not be implemented. To handle this situation, we have to do it with inverted single quotes.</p><p><br/></p><p><code>fdv=`date +%F`echo &quot;Today $fdv&quot;</code></p><p><br/></p><p>date +% in backquotes F will be regarded as an instruction, and the execution result will be brought into the fdv variable.</p><p><strong>, comma (comma, comma in punctuation)</strong></p><p><br/></p><p><code>This symbol is often used in operations as a &quot;separation&quot; use. Example touch f{1,2,3}</code></p><p><br/></p><p><strong>/ slash (forward slash): </strong></p><p>Indicated in the path , representing a directory. Usually a single / represents the meaning of the root root directory</p><p>The slash &quot;/&quot; refers to the path directory separator, there is nothing to say here.</p><p><br/></p><p><code>ls ~/work/tests/</code></p><p><br/></p><p><br/></p><p>However, interestingly, if / If there is nothing in front of the path directory separator, does it mean that this is the top-level directory? Since the directory tree of the Linux system starts with /, only one / represents the system root directory we often say.</p><p><br/></p><p><code>cd /</code></p><p><br/></p><p><br/></p><p><strong> Backslash (escape character)&lt; /strong&gt;</strong></p><p>The escape character in interactive mode has several functions; it can cancel aliases if it is placed before the command; At the very end of an instruction, it means that the instruction connects to the next line.</p><p><br/></p><p><code># type rmrm is aliased to `rm -i&#39;# rm ./*.log</code></p><p><br/></p><p>The above example, I added the escape character before the rm command to temporarily cancel the function of the alias and restore the rm command.</p><p><br/></p><p><code># bkdir=/home# echo &quot;Backup dir, $bkdir = $bkdir&quot;Backup dir, $bkdir = /home</code></p><p><br/></p><p>The $bkdir in the echo in the above example, escape cancels the function of the $ variable, so $bkdir will be output, and the second $bkdir will output the variable content /home.</p><p><strong>| Pipeline (pipeline)</strong></p><p>Pipeline is a UNIX system, a basic and important concept. Connect the standard output of the previous command as the standard input of the next command.</p><p><br/></p><p><code>who | wc -l</code></p><p><br/></p><p>Using this concept well is quite helpful for streamlining scripts</p><p><strong>! Exclamation point (negate or reverse)</strong></p><p>Usually represents the role of anti-logic, such as condition detection, != to represent &quot;not equal to&quot;</p><p>ls f[!1-9] means to display other files except f0, f1 .... f9</p><p><strong>:colon</strong></p><p>In bash, this is a builtin: &quot;do nothing&quot;, but returns status value 0</p><p><br/></p><p><code>echo $? # echo is 0 : &gt; f.$$</code></p><p><br/></p><p>The above line is equivalent to cat /dev/null &gt;f.$$. Not only is the writing shorter, but the execution efficiency is also much better.</p><p>Sometimes, the following usages also appear</p><p><br/></p><p><code>: ${HOSTNAME?} ${USER?} ${MAIL?}</code></p><p><br/></p><p>The function of this line is to check whether these environment variables have been set, and if they are not set, an error message will be displayed on standard error. This kind of check can basically be handled if you use methods like test or if, but they are not as concise and efficient as the above example.</p><p>In addition to the above, there is another place where colons must be used</p><p><br/></p><p><code>PATH=$PATH:$HOME/fbin:$HOME/fperl:/usr/ local/mozilla</code></p><p><br/></p><p>In the .bash_profile or any file with similar functions in the user&#39;s own HOME directory, when setting the &quot;path&quot;, we all use A colon is used as a separator.</p><p><strong>? Question mark (wild card)</strong></p><p>The role played by the filename expansion (Filename expansion) is to match an arbitrary character, but does not contain null character.</p><p><br/></p><p><code># ls a?a1</code></p><p><br/></p><p><strong>Asterisk (wild card)</strong></p><p>A fairly common symbol. On filename expansion (Filename expansion), she is used to represent any character, including the null character.</p><p><br/></p><p><code># ls a*a a1 access_log</code></p><p><br/></p><p>In operation, it represents &quot;multiplication&quot;.</p><p><br/></p><p><code>let &quot;fmult=2*3&quot;</code></p><p><br/></p><p>In addition to the built-in instruction let, there is also an instruction expr about operation , the asterisk also plays the role of &quot;multiplication&quot; here. But be careful in use, he must be preceded by an escape character.</p><p><strong>** Power operation</strong></p><p>Two asterisks represent &quot;power&quot; in operation.</p><p><br/></p><p><code>let &quot;sus=2**3&quot; echo &quot;sus = $sus&quot; # sus = 8</code></p><p><br/></p><p><strong>$$</strong></p><p>The representative symbol of variable substitution (Variable Substitution).</p><p><br/></p><p><code>vrs=123echo &quot;vrs = $vrs&quot; # vrs = 123</code></p><p><br/></p><p>In addition, it is defined in Regular Expressions as The end-of-line of the &quot;line&quot;. This is commonly used in grep, sed, awk, and vim(vi).</p><p><strong>Regular Expressions for ${} Variables</strong></p><p>bash defines a number of uses for ${}. The following is a table taken from the online description <br/></p><p><br/></p><p><code>${parameter:-word} ${parameter:=word} ${parameter:?word} ${ parameter:+word} ${parameter:offset} ${parameter:offset:length} ${!prefix*} ${#parameter} ${parameter#word} ${parameter##word} ${parameter%word} $ {parameter%%word} ${parameter/pattern/string} ${parameter//pattern/string}</code></p><p><br/></p><p><strong>$*</strong></p><p>$* Refers to the execution reference variable of the script. The algorithm of referencing parameters is the same as that of general instructions. The instruction itself is 0, followed by 1, and so on. Reference variables are represented as follows:</p><p><br/></p><p><code>$0, $1, $2, $3, $4, $5, $6, $7, $8, $9, ${10}, ${11}. ....</code></p><p><br/></p><p>For single digits, you can use numbers directly, but for more than two digits, you must use {} symbols to enclose</p><p>$* is a symbol representing all reference variables. When used, double quotes may be added as appropriate.</p><p><br/></p><p><code>echo &quot;$*&quot;</code></p><p><br/></p><p>There is also a symbol that has the same effect as $*, but has a slightly different effect and processing method</p><p><strong>¥@</strong></p><p>$@ and $ * Symbols that have the same effect, but they have one difference.</p><p>The symbol $* treats all quoted variables as a whole.</p><p>But the symbol $@ still retains the concept of a section for each referenced variable.</p><p><strong>$#</strong></p><p>This is also a symbol related to reference variables. Her function is to tell you the total number of reference variables.</p><p><br/></p><p><code>echo &quot;$#&quot;</code></p><p><br/></p><p><strong>$? Status value (status variable)</strong></p><p>Generally speaking, the process of UNIX (linux) system ends by executing the system call exit(). This return value is the status value. Passed back to the parent process to check the execution status of the child process.</p><p>If the general command program is executed successfully, its return value is 0; if it fails, it is 1.</p><p><br/></p><p><code>tar cvfz dfbackup.tar.gz /home/user &gt; /dev/nullecho &quot;$?&quot;$$</code></p><p><br/></p><p>Since the ID of the process is unique, it is impossible to have duplicate PIDs at the same time. Sometimes, the script will need to generate temporary files to store necessary data. And this script may also be used by users at the same time. In this case, the fixed file name is obviously unreliable in writing. Only by generating a dynamic file name can it meet the needs. The symbol $$ may meet this requirement. It represents the PID of the current shell.</p><p><br/></p><p><code>echo &quot;$HOSTNAME, $USER, $MAIL&quot; &gt; ftmp.$$</code></p><p><br/></p><p>Use this as the filename Part of it can avoid overwriting with the same file name at the same time.</p><p>ps: Basically, the system will recycle the PID after execution, and then allocate it again as needed. Therefore, even if the script uses a dynamic file name for the temporary file, if the script is not cleared after the script is executed, other problems will occur.</p><p><strong>( ) command group (command group)</strong></p><p>Use brackets to enclose a series of consecutive commands, this usage is called for the shell command group. For example: (cd ~ ; vcgh=`pwd`;echo $vcgh), the command group has a feature, the shell will execute this group of commands by generating a subshell. Therefore, the variables defined in it only act on the command group itself. Let&#39;s see an example</p><p><br/></p><p><code># cat ftmp-01#!/bin/basha=fsh(a=incg ; echo -e &quot;n $a n&quot;)echo $ a#./ftmp-01incgfsh</code></p><p><br/></p><p>In addition to the above command groups, brackets are also used in the definition of array variables; in addition, they are also used in other possible escape characters It can only be used in occasions, such as calculation expressions.</p><p><strong>(( ))</strong></p><p>The role of this group of symbols is similar to the let command, which is used in arithmetic operations and is a built-in function of bash. Therefore, the execution efficiency will be much better than using the let instruction.</p><p><br/></p><p><code>#!/bin/bash(( a = 10 ))echo -e &quot;inital value, a = $an&quot;(( a++))echo &quot;after a++, a = $a&quot;</code></p><p><br/></p><p><strong>{ } Braces (Block of code)</strong></p><p>Sometimes they will appear in the script, There will be one or more instructions or variable settings ending in &quot;semicolons&quot; between the braces.</p><p><br/></p><p><code># cat ftmp-02#!/bin/basha=fsh{a=inbc ; echo -e &quot;n $a n&quot;}echo $a#./ftmp- 02inbcinbc</code></p><p><br/></p><p>This usage is very similar to the command group introduced above, but there is a difference, it is executed in the current shell and will not generate a subshell.</p><p>Braces are also used for &quot;function&quot; functions. Broadly speaking, simply using curly braces acts like a function without a specified name. Therefore, writing scripts like this is also a pretty good thing. Especially for the redirection of output and input, this approach can simplify the complexity of the script.</p><p>In addition, braces have another usage, as follows</p><p><br/></p><p><code>{xx,yy,zz,...}</code></p><p><br/></p><p>This combination of curly brackets is often used in the combination of strings, let’s see an example</p><p><br/></p><p><code>mkdir {userA,userB,userC}-{home,bin,data}</code></p><p><br/></p><p>We get userA-home, userA-bin, userA-data, userB-home, userB-bin, userB-data, userC -home, userC-bin, userC-data, these directories. This set of symbols is quite broad in applicability. Put to good use, the rewards are simplicity and efficiency. Like the following example</p><p><br/></p><p><code>chown root /usr/{ucb/{ex,edit},lib/{ex?.?*,how_ex}}</code></p><p><br/></p><p>If it is not because of supporting this usage, we have to write a few lines and repeat it several times</p><p><strong>[ ] square brackets</strong></p><p>It often appears in process control and plays the role of enclosing judgment. if [ &quot;$?&quot; != 0 ]thenecho &quot;Executes error&quot; exit1fi</p><p>This symbol plays a role similar to &quot;range&quot; or &quot;set&quot; in regular expressions</p><p><br/></p><p><code>rm -r 200[1234]</code></p><p><br/></p><p>The above example means deleting 2001, 2002, 2003, 2004 and other directories</p><p>&lt; strong&gt;[[ ]]</p><p>This group of symbols has basically the same function as the previous [] symbols, but she allows symbols such as || and &amp;&amp; logic to be used directly.</p><p><br/></p><p><code>#!/bin/bashread akif [[ $ak &gt; 5 || $ak&lt; 9 ]]thenecho $akfi</code></p><p><br/></p><p><strong>|| logic symbol</strong></p><p>This will often be seen, representing the symbol of or logic<br/></p><p><strong>&amp;&amp; logic symbol</strong></p><p>This is also often seen, representing the symbol of and logic<br/></p><p><strong>&amp; background work</strong></p><p>list An &amp; symbol placed at the end of a complete command line means that the command line will be put into the background to work.</p><p><br/></p><p><code>tar cvfz data.tar.gz data &gt; /dev/null&amp;</code></p><p><br/></p><p><strong>&lt;…&gt;word boundary&lt; /strong&gt;<!--…--></strong></p><p>This group of symbolsIn regular expressions, it is defined as the meaning of &quot;boundary&quot;. For example, when we want to find the word the, if we use</p><p><br/></p><p><code>grep the FileA</code></p><p><br/></p><p>you will find, Words like there are also considered matching words. Because the happens to be part of there. If we want to avoid this situation, we have to add the &quot;boundary&quot; symbol</p><p><br/></p><p><code>grep &#39;&#39; FileA</code></p><p><br/></p><ul class=" list-paddingleft-2"><li><p>Plus (plus)</p></li></ul><p><br/></p><p><code>In the expression, she is used to represent &quot;addition&quot;. expr 1 + 2 + 3 In addition, in regular expressions, it is used to represent the meaning of the preceding characters of &quot;many&quot;. # grep &#39;10+9&#39; fileB109100910000910000931010009# When this symbol is used, the escape character must be added in front of it.</code></p><p><br/></p><ul class=" list-paddingleft-2"><li><p>minus sign (dash)</p></li></ul><p><br/></p><p><code>in the expression In, she is used to mean &quot;subtraction&quot;. expr 10 - 2 It is also an option symbol for system commands. ls -expr 10 - 2 In the GNU command, if the - symbol is used alone without adding any file name that should be added, it means &quot;standard input&quot;. This is a common option for GNU commands. For example, the following example tar xpvf - The - symbol here means reading data from standard input. However, it is special in the cd command cd - This means changing the working directory to the &quot;last&quot; working directory.</code></p><p><br/></p><p><strong>% Division (Modulo)</strong></p><p>In the expression, it is used to represent &quot;division&quot;.</p><p><br/></p><p><code>expr 10 % 2</code></p><p><br/></p><p>In addition, it is also used in the following regular expressions about variables</p><p><br/></p><p><code>${parameter%word}${parameter%%word}</code></p><p><br/></p><p>One % means the shortest word match, two means the shortest Long word matches.</p><p><strong>= Equals sign (Equals)</strong></p><p>A symbol that is often seen when setting variables.</p><p><br/></p><p><code>vara=123echo &quot; vara = $vara&quot;</code></p><p><br/></p><p>Or like PATH settings, even applied to calculations or judgment expressions.</p><p><strong>== The equal sign (Equals)</strong></p><p>is often seen in the conditional judgment formula, which means &quot;equal to&quot;.</p><p><br/></p><p><code>if [ $vara = = $varb ]... abbreviated</code></p><p><br/></p><p><strong>! = not equal to</strong></p><p>often seen in conditional judgments , stands for &quot;not equal to&quot;.</p><p><br/></p><p><code>if [ $vara != $varb ]...abbreviated</code></p><p><br/></p><p><strong>^</strong></p><p>This symbol represents the &quot;beginning&quot; position of the line in the regular expression, and it is also the same as &quot;!&quot; (exclamation mark) in [] to indicate non<br/></p><p><strong>Output/Input Redirection</strong></p><blockquote><p>&gt; &gt;&gt; &lt; &lt;&lt; :&gt; &amp;&gt; 2&amp;&gt; 2&lt;&gt;&gt;&amp; &gt;&amp;2&lt; /p&gt;</p></blockquote><p>File Descriptor (File Descriptor), use a number (usually 0-9) to represent a file.</p><p>Common file descriptors are as follows:</p><p><br/></p><p><code>File Descriptor Name Common Abbreviations Default 0 stdin stdin keyboard 1 stdout stdout screen 2 Standard Error Output stderr Screen</code></p><p><br/></p><p>When we simply use &lt; or &gt;, it is equivalent to using 0&lt; or 1&gt; (described in detail below).</p><ul class=" list-paddingleft-2"><li><p>* cmd &gt; file</p><p>Redirect the output of the cmd command to the file file. If the file already exists, clear the original file, and use the noclobber option of bash to prevent the original file from being overwritten.</p></li><li><p>* cmd &gt;&gt; file</p><p>Redirect the output of the cmd command to the file file, if the file already exists, add the information to the original file later.</p></li><li><p>* cmd &lt; file</p><p>Make cmd command read from file</p></li><li><p>* cmd &lt;&lt; text</p><p>From the command line Read input until an end of line identical to text. This mode performs shell variable substitution on the input unless the input is enclosed in quotes. If you use &lt;&lt;-, the tab at the beginning of the next input line will be ignored, and the end line can also be a bunch of tabs plus a content that is the same as text, you can refer to the following examples.</p></li><li><p>* cmd &lt;&lt;&lt; word</p><p>Provide word (not the file word) and the following newline as input to cmd.</p></li><li><p>* cmd &lt;&gt; file</p><p>Redirect the file file to the input in read-write mode, the file file will not be destroyed. It only makes sense if the application takes advantage of this feature.</p></li><li><p>* cmd &gt;| file</p><p>The function is the same as&gt;, but even when noclobber is set, the file file will be overwritten. Note that | is used instead It is not mentioned in some books! Currently, &gt;! is still used only in csh to realize this function.</p></li><li><p>: &gt; filename Truncate the file &quot;filename&quot; to 0 length. # If the file does not exist, create a 0-length file (same effect as &#39;touch&#39;) .</p></li><li><p>cmd &gt;&amp;n Send output to file descriptor n</p></li><li><p>cmd m&gt;&amp;n Send output to file descriptor m redirect information to file descriptor n</p></li><li><p>cmd &gt;&amp;- close standard output</p></li><li><p>cmd &lt;&amp;n input from file The descriptor n</p></li><li><p>cmd m&lt;&amp;n m comes from the file description of each n</p></li><li><p>cmd &lt;&amp;- close standard input</p></li><li><p>cmd &lt;&amp;n- Move input file descriptor n instead of copying it. (Explanation required)</p></li><li><p>cmd &gt;&amp;n- Move output file descriptor n instead of copying it. (needs explanation)</p></li></ul><p>Note: &gt;&amp; actually duplicates the file descriptor, which makes cmd &gt; file 2&gt;&amp;1 not the same as cmd 2&gt;&amp;1 &gt;file .</p><p><br/></p>


Copyright Description:No reproduction without permission。

Knowledge sharing community for developers。

Let more developers benefit from it。

Help developers share knowledge through the Internet。

Follow us