Regular expressions use meta-characters. The shells also have meta-characters.Meta-characters are simply characters that have a special meaning.The problem occurs when you want to use a regular expression in a shell script. Will the shell do something special with the character?Or will it be passed unchanged to the program?The "$" character is a good example.It could be the beginning of a variable name, or it could be part of a regular expression.If you need a regular expression, you must know if any of the characters of the expression are meta-characters, and must know the right way to quote that character, so that it is passed to the program without being modified by the shell.
Here is a chart of the meta-characters the Bourne and C shell know about.I have also included several combinations of characters just to make this table more complete. There is a lot of detail on this chart.
Character | Where | Meaning |
<RETURN> | csh, sh | Execute command |
# | csh, sh, ASCII files | Start a comment |
<SPACE> | csh, sh | Argument separator |
` | csh, sh | Command substitution |
" | csh, sh | Weak Quotes |
' | csh, sh | Strong Quotes |
\ | csh, sh | Single Character Quote |
variable | sh, csh | Variable |
variable | csh, sh | Same as variable |
| | csh, sh | Pipe character |
^ | sh | Pipe Character |
& | csh, sh | Run program in background |
? | csh, sh | Match one character |
* | csh, sh | Match any number of characters |
; | csh, sh | Command separator |
;; | sh | End of Case statement |
~ | csh | Home Directory |
~user | csh | User's Home Directory |
! | csh | History of Commands |
- | Programs | Start of optional argument |
$# | csh, sh | Number of arguments to script |
$* | csh, sh | Arguments to script |
$@ | sh | Original arguments to script |
$- | sh | Flags passed to shell |
$? | sh | Status of previous command |
$$ | sh | Process identification number |
$! | sh | PID of last background job |
&& | sh | Short-circuit AND |
|| | sh | Short-circuit OR |
. | csh, sh | Typ. filename extension |
. | sh | Source a file and execute as command |
: | sh | Nothing command |
: | sh | Separates Values in environment variables |
: | csh | Variable modifier |
Character | Where | Meaning |
[ ] | csh, sh | Match range of characters |
[ ] | sh | Test |
%job | csh | Identifies job Number |
(cmd;cmd) | csh. sh | Runs cmd;cmd as a sub-shell |
{ } | csh | In-line expansions |
{cmd;cmd } | sh | Like (cmd;cmd ) without a subshell |
>file | csh, sh | Standard output |
>>file | csh, sh | Append to standard output |
<file | csh, sh | Standard Input |
<<word | csh, sh | Read until word, substitute variables |
<<\word | csh, sh | Read until word, no substitution |
<<-word | sh | Read until word, ignoring TABS |
>>!file | csh | Append to file, ignore error if not there |
>!file | csh | Output to new file, ignore error if not there |
>&file | csh | Send standard & error output to file |
<&digit | sh | Switch Standard Input to file |
<&- | sh | Close Standard Input |
>&digit | sh | Switch Standard Output to file |
>&- | sh | Close Standard Output |
digit1<&digit2 | sh | Connect digit2 to digit1 |
digit<&- | sh | Close file digit |
digit2>&digit1 | sh | Connect digit2 to digit1 |
digit>&- | sh | Close file digit |
这两天我一直纠结关于正则表达式在emacs,grep,find中何时该加引号,何时不该加引号的问题。虽然我前面的两篇博客“浅谈lisp与shell中转义字符和正则表达”也基本上理清了,因为本身shell/emacs会有一些特殊字符我们称为Meta-Character,而正则表达式也是利用Meta-Character实现的;所以说这个时候就会有一些情况,两个字符在双方都是Meta-character,因为我们既然是正则表达的话,肯定是传给正则表达它所想要的字符形式,但是如果你在shell这边穿过去的时候,它的形式变了。比如$test,你传给正则的不是形式$test而是它的值。所以这就需要进行引号引用或转义了。
唉,我感觉我前两篇写的核心思想,被这个哥们上面的几句话,给结束了;太受挫了。悲了个剧!!!!最后看到它的简介以后心里平衡了,原来是出过书的大牛。。。
Meta-Character in a Unix command that the shell interprets rather than passing to the command也就是说shell碰见下面的这些符号的话,不会说把它传给Command,而是自己对它进行解析。
比如下面[] 和?经过echo值仍旧是[et]?,所以我们就可以安心的传给正则表达式函数了,而不需要加引号。具体是否变化可以通过echo来检验
The Grymoire - home for UNIX wizards