首頁  >  文章  >  運維  >  總結bash常用特殊字符

總結bash常用特殊字符

巴扎黑
巴扎黑原創
2017-08-17 10:31:381644瀏覽

#註解符號(Hashmark)

1.在shell檔案的行首,作為shell呼叫解釋器標記,#!/bin/bash;

2. 在設定檔中作為註解使用,在一行中,#後面的內容並不會被執行

;

作為多指令的分隔符號(Command separator [semicolon])。

多個指令要放在同一行的時候,可以使用分號分隔。

;;

連續分號(Terminator [double semicolon])。

在使用case選項的時候,作為每個選項的終結符。

.

點號(dot command)。

1. 執行目前目錄檔案

         #!/bin/bash

         .pythontab-file

2. 作為檔案名稱的一部分,在檔案名稱中名的開頭,表示該檔案為隱藏檔案

3. 作為目錄名,一個點代表目前目錄,兩個點號代表上層目錄(目前目錄的父目錄)。請注意,兩個以上的點不出現,除非你用引號(單/雙)包圍作為點號字元本身;

4. 正規表示式中,點號表示任意一個字元。

"

雙引號(double quotation marks)。

部分引用。雙引號包圍的內容可以允許變數擴展,也允許轉義字元的存在。

'

單引號(full quoting [single quote])。 (comma operator [comma])。

#         #!/bin/bash

         lett1=((a=5+1, b=7+2))

        echot1$ =$b

         ##這個$t1=$b;

\

反斜線,反斜桿(escape [backslash])。 1. 放在特殊符號之前,轉義特殊符號的作用,僅表示特殊符號本身,這在字符串中常用;

2. 放在一行指令的最末端,表示緊接著的回車無效(其實也就是轉義了Enter),後繼新行的輸入仍然作為目前指令的一部分。 。運算子的時候,表示除法符號。 。 ,此處Mitchell特地使用了反引號和單引號,注意區別。都不做,但是有回傳值,回傳值為0(即:true)。在if分支中作為佔位符(即某一分支什麼都不做的時候);

3. 放在必須要有兩元操作的地方作為分隔符,如::${username= `whoami`}

4. 在參數替換中為字串變數賦值,在重定向操作(>)中,把一個檔案長度截斷為0(:>>這樣用的時候,目標存在則什麼都不做),這個只能在普通文件中使用,不能在管道,符號鏈接和其他特殊文件中使用;

5. 甚至你可以用來註釋(#後的內容不會被檢查,但:後的內容會被檢查,如果有語句如果出現語法錯誤,則會報錯);

6. 你也可以作為域分隔符,比如環境變量$PATH中,或者在passwd中,都有冒號的作為域分隔符號的存在;

7. 你也可以將冒號作為函數名,不過這個會將冒號的本來意義轉變(如果你不小心作為函數名,你可以使用unset -f : 來取消function的定義)。

!

感嘆號(reverse (or negate) [bang],[exclamation mark])。

取反一個測試結果或退出狀態。

1. 表示反邏輯,例如後面的!=,這個是表示不等於;

2. 表示取反,如:ls a[!0-9] #表示a後面不是緊接著一個數字的檔案;

3. 在不同的環境裡面,感嘆號也可以出現在間接變數引用裡面;

4. 在命令列中,可以用於歷史指令機制的調用,你可以試試!$,!#,或者!-3看看,不過要注意,這點特性不能在腳本文件裡面使用(被禁用)。

*

星號(wildcard/arithmetic operator[asterisk])。

1. 作為匹配檔案名稱擴展的一個通配符,能自動匹配給定目錄下的每一個檔案;

2. 正規表示式中可以作為字元限定符,表示其前面的符合規則符合任意次;

3. 算術運算中表示乘法。

**

雙星號(double asterisk)。算術運算中表示求冪運算。

?

問號(test operator/wildcard[Question mark])。

1. 表示條件測試;

2. 在雙括號內表示C風格的三元運算子((condition?true-result:false-result));

3. 參數替換表達式中用來測試一個變數是否設定了值;

4. 作為通配符,用於匹配檔案名稱擴展特性中,用於匹配單一字元;

5. 正規表示式中,表示符合其前面規則0次或1次。

$

美元符號(Variable substitution[Dollar sign])。

1. 作為變數的前導符,用作變數替換,即引用一個變數的內容,例如:echo $PATH;

2. 在正規表示式中被定義為行末( End ofline)。

${}

參數替換(Variable substitution)。

用於在字串中表示變數。

$'...'

引用內容展開,執行單引號內的轉義內容(單引號原本是原樣引用的),這種方式會將引號內的一個或者多個[\]轉義後的八進制,十六進位值展開到ASCII或Unicode字元。

$*

$@

位置參數(Positional Parameters)。

這個在使用腳本檔案的時候,傳遞參數的時候會用到。兩者都能傳回呼叫腳本檔案的所有參數,但$*是將所有參數作為一個整體傳回(字串),而$@ 是將每個參數作為單元傳回一個參數清單。請注意,在使用的時候需要用雙引號將$*,$@括住。這兩個變數受到$IFS的影響,如果在實際應用中,要考慮其中的一些細節。

$

#表示傳遞給腳本的參數數量。

$?

此變數值在使用的時候,傳回的是最後一個指令、函數、或腳本的退出狀態碼值,如果沒有錯誤則是0,如果為非0 ,則表示在此之前的最後一次執行有錯誤。

$$

進程ID變量,這個變數保存了執行目前腳本的進程ID值。

()

圓括號(parentheses)。

1, 命令群組(Command group)。由一組圓括號括起來的指令是指令組,指令組中的指令實在子shell(subshel​​l)中執行。因為是在子shell內運行,因此在括號外面是沒有辦法獲取括號內變量的值,但反過來,命令組內是可以獲取到外面的值,這點有點像局部變量和全局變量的關係,在實作中,如果碰到要cd到子目錄操作,並在操作完成後要返回當前目錄的時候,可以考慮使用subshel​​l來處理;

2. 用於數組的初始化。

{x,y,z,...}

花括號擴充(Brace Expansion)。

在指令中可以用這種擴充來擴充參數列表,指令將會依照列表中的括號分隔開的模式進行比對擴充。注意的一點是,這花括號擴展中不能有空格存在,如果確實有必要空格,則必須被轉義或使用引號來引用。例:echo {a,b,c}-{\ d," e",' f'}

#{a..z}

在Bash version 3時加入了這種花括號擴展的擴展,可以使用{A..Z}表示A-Z的所有字符列表,這種方式的擴展Mitchell測試了一下,好像僅適用於A-Z,a-z,還有數字{最小..最大}的這種方式擴展。

{}

程式碼區塊(curly brackets)。

這個是匿名函數,但是又與函數不同,在程式碼區塊裡面的變數在程式碼區塊後面仍能存取。注意:花括號內側需要有空格與語句分隔。另外,在xargs -i中的話,還可以作為文本的佔位符,用以標記輸出文本的位置。

{} \;

這個{}是表示路徑名,這個不是shell內建的,現在接觸到的情況看,好像只用在find指令裡。注意後面的分號,這個是結束find指令中-exec選項的指令序列,在實際使用的時候,要轉義一下以免被shell理解錯誤。

[]

中括號(brackets)。

1. 測試的表示,Shell會測試在[]內的表達式,需要注意的是,[]是Shell內建的測試的一部分,而不是使用外部命令/usr/bin/test的連結;

2. 在陣列的脈絡中,表示陣列元素,方括號內填入陣列元素的位置就能獲得對應位置的內容,如:

1.      Array[ 1]=xxx

2.      echo${Array[1]};

3. 表示字元集的範圍,在正表達式中,方括號表示該位置可以匹配的字符集範圍。

[[]]

雙中括號(double brackets)。

這個結構也是測試,測試[[]]之中的表達式(Shell的關鍵字)。這個比單中括號更能防止腳本裡面的邏輯錯誤,例如:&&,||,<,>運算子能在一個[[]]裡面測試通過,但在[]卻不能通過。 [[]]裡面沒有檔案名稱擴充(filename expansion)或是字分隔符號(Word splitting),但是可以用參數擴充(Parameter expansion)和指令來取代(command substitution)。不用檔案名稱通配符和像空白這樣的分隔符號。注意,這裡面如果出現了八進制,十六進制等,shell會自動執行轉換比較。

$[...]

The word expression represents integer expansion.

Execute integer expressions inside square brackets. Example:

1. a=3

2. b=7

3. echo$[$a+$b]

4. echo$ [$a*$b]

5. ##The return is 10 and 21

(())

double parentheses.

represents integer expansion.

In the double bracket structure, all expressions can be like C language, such as: a++, b--, etc.

In the double bracket structure, all variables do not need to be prefixed with the "$" symbol.

Double brackets can perform logical operations and four arithmetic operations

The double bracket structure extends the for, while, and if condition test operations

Supports multiple expression operations, each expression Use "," to separate

>

&<

>&

>>

<

<>

Redirection.

scriptname >filename redirects the output of scriptname to the file filename, and overwrites the original file if the filename file exists;

command &>filename redirects the standard output (stdout) of command and Standard error (stderr) to the file filename;

command >&2 Redirect the standard output (stdout) of command to the standard error (stderr);

scriptname >>filename Append the output of scriptname (same as >) to the file filename, or create the file if it does not exist. One > indicates that the file will be overwritten if it exists, and created if it does not exist. Two >> indicates that it will be created if it does not exist. If it exists, it will be added to the original one.

[i]<>filename Open filename The file is used for reading or writing, and i is assigned to the file as its file descriptor (file descriptor). If the file does not exist, it will be created.

<<

Double less then marks (here-document[double less then marks]).

This is also called Here-document and is used to redirect subsequent content to the stdin of the left command. <

<<<

Three less than signs (here-strings). Here-string is similar to Here-document, here-strings syntax: command [args] <<<["]$word["]; $word will be expanded and used as the stdin of command.

\<...\>

Word boundary.

This is a special delimiter used in regular expressions to mark word boundaries. For example: the will match there, another, them, etc. If you only want to match the, you can use this word boundary character, \ will only match the.

|

Pipe. Pipes are a concept that exists in both Linux and Unix. It is a very basic and important concept. Its function is to use the output (stdout) produced by the command before the pipe (on the left) as the input (stdin) of the command after the pipe (on the right). For example: ls | wc l, you can use pipes to connect commands together. Note: The standard output of each process in a pipe will be used as the standard input of the next command. The standard output during the period cannot cross the pipe and be used as the standard input of the subsequent command, such as: cat filename | ls -al | sort. Think about the output of this? Also, the pipe runs as a child process, so the pipe cannot cause variables to change.

>|

Force redirection.

This will force overwriting of already existing files.

&

Run job in background[ampersand]).

If the command is followed by an & symbol, the command will run in the background.

&&

||

Logical operator.

In the test structure, these two operators can be used to connect two logical values. || returns 0 (true) when one of the test conditions is true, and false if all are false; && returns true (0) when both test conditions are true, and false if any.

-

Minus sign, hyphen (Hyphen/minus/dash).

1. As an option, the prefix [option, prefix] is used.

2. Source or destination for stdin or stdout redirection [dash]. When tar does not have bunzip2 program patch, we can do this: bunzip2 linux-2.6.13.tar.bz2 | tar xvf - . Use the previously decompressed data as the standard input of tar (a - is used here)

Note: During implementation, if the file name starts with [-], then add this as a directional operation When echoing a variable, an error may occur. At this time, a suitable prefix path should be added to the file to avoid this situation. Similarly, when echoing a variable, if the variable starts with [-], it may also occur. Unexpected results. To be on the safe side, you can use double quotes to quote scalars:

var="-n"

echo$var

var="-n"

echo$var

Try to see what the output is?

Also, this representation method is not built-in in Bash. To achieve this effect, you need to see whether the software you are using supports this operation;

3. Represents the previous Therefore, if you cd to another directory and want to put back the previous path, you can use cd - to achieve the purpose. In fact, the [-] here uses the environment variable $OLDPWD. Note: [-] here is different from the previous point;

4. The minus sign or negative sign is used in arithmetic operations.

=

Equals.

1. When assigning a value to a variable, is there any space on both sides of the equal sign?

2. Appears as a comparison operator in a comparison test. Note here that if it is in square brackets To appear as a comparison, there need to be spaces on both sides of the equal sign.

+

Plus.

1. Arithmetic operator, indicating addition;

2. In a regular expression, it means that the preceding matching rule matches at least once;

3. As an option marker in a command or filter, use + in some commands or built-in commands to enable certain options, and use - to disable;

4. In parameter substitution (parametersubstitution), the + prefix means Substitute value (when the variable is empty, use the value after +)

%

percent sign (modulo[percent sign]).

1. In arithmetic operations, this is the modulus operator, which is the remainder after division of two numbers;

2. In parameter substitution (parametersubstitution), it can be used as a pattern match. Example:

                                                                                                                                                                                                                                                                                      to #       ##Start searching from the right (think about which symbol is from the left?)

      ##Any content between b and 9 (inclusive)

        ##The first one is to find The shortest matching item

        ##The last one is to find the largest matching item (greedy matching?)

~

tilde (Home directory[tilde]).

This is the same as the internal variable $HOME. By default, it indicates the current user's home directory (home directory). This has the same effect as ~/. If the tilde is followed by the user name, it indicates the user's home directory.

^

caret.

1. In regular expressions, as the beginning-of-line position identifier of a line;

2. In parameter substitution (Parametersubstitution), this usage has two ways One caret (${var^}), or two (${var^^}), respectively means the first letter is capitalized, and all capitals means (Bash version >=4).

Blank

Whitespace.

White space not only refers to spaces (spaces), but also includes tabs (tabs), blank lines (blank lines), or a combination of these. It can be used as a function delimiter to separate commands or variables. A blank line will not affect the behavior of the script, so it can be used to plan the script code to increase readability. The built-in special variable $IFS can be used to target certain commands. The input parameters are split, and the default is the whitespace character. If there are whitespace characters in a string or variable, you can use quotes to avoid possible errors.

以上是總結bash常用特殊字符的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn