📄 special-chars.html
字号:
></TR></TABLE></P></TD></TR></TABLE></DIV><DIVCLASS="FORMALPARA"><P><B>array initialization. </B><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING"> 1 Array=(element1 element2 element3)</PRE></TD></TR></TABLE></P></DIV></DD><DT><SPANCLASS="TOKEN">{xxx,yyy,zzz,...}</SPAN></DT><DD><DIVCLASS="FORMALPARA"><P><B>Brace expansion. </B><ANAME="BRACEEXPREF"></A><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING"> 1 cat {file1,file2,file3} > combined_file 2 # Concatenates the files file1, file2, and file3 into combined_file. 3 4 5 cp file22.{txt,backup} 6 # Copies "file22.txt" to "file22.backup"</PRE></TD></TR></TABLE></P></DIV><P>A command may act upon a comma-separated list of file specs within <TTCLASS="REPLACEABLE"><I>braces</I></TT>. <ANAME="AEN1000"HREF="#FTN.AEN1000">[3]</A> Filename expansion (<AHREF="globbingref.html">globbing</A>) applies to the file specs between the braces.</P><DIVCLASS="CAUTION"><TABLECLASS="CAUTION"WIDTH="90%"BORDER="0"><TR><TDWIDTH="25"ALIGN="CENTER"VALIGN="TOP"><IMGSRC="common/caution.png"HSPACE="5"ALT="Caution"></TD><TDALIGN="LEFT"VALIGN="TOP"><P>No spaces allowed within the braces <SPANCLASS="emphasis"><ICLASS="EMPHASIS">unless</I></SPAN> the spaces are quoted or escaped.</P><P><TTCLASS="USERINPUT"><B>echo {file1,file2}\ :{\ A," B",' C'}</B></TT></P><P><TTCLASS="COMPUTEROUTPUT">file1 : A file1 : B file1 : C file2 : A file2 : B file2 : C</TT></P></TD></TR></TABLE></DIV></DD><DT><ANAME="BRACEEXPREF33"></A><SPANCLASS="TOKEN">{a..z}</SPAN></DT><DD><DIVCLASS="FORMALPARA"><P><B>Extended Brace expansion. </B><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING"> 1 echo {a..z} # a b c d e f g h i j k l m n o p q r s t u v w x y z 2 # Echoes characters between a and z. 3 4 echo {0..3} # 0 1 2 3 5 # Echoes characters between 0 and 3.</PRE></TD></TR></TABLE> </P></DIV><P>The <ICLASS="FIRSTTERM">{a..z}</I> <AHREF="bashver3.html#BRACEEXPREF3">extended brace expansion</A> construction is a feature introduced in <AHREF="bashver3.html#BASH3REF">version 3</A> of <ICLASS="FIRSTTERM">Bash</I>.</P></DD><DT><ANAME="CODEBLOCKREF"></A><SPANCLASS="TOKEN">{}</SPAN></DT><DD><DIVCLASS="FORMALPARA"><P><B>Block of code [curly brackets]. </B>Also referred to as an <ICLASS="FIRSTTERM">inline group</I>, this construct, in effect, creates an <ICLASS="FIRSTTERM">anonymous function</I> (a function without a name). However, unlike in a <SPANCLASS="QUOTE">"standard"</SPAN> <AHREF="functions.html#FUNCTIONREF">function</A>, the variables inside a code block remain visible to the remainder of the script.</P></DIV><P> <TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="SCREEN"> <TTCLASS="PROMPT">bash$ </TT><TTCLASS="USERINPUT"><B>{ local a; a=123; }</B></TT> <TTCLASS="COMPUTEROUTPUT">bash: local: can only be used in afunction</TT> </PRE></TD></TR></TABLE> </P><P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING"> 1 a=123 2 { a=321; } 3 echo "a = $a" # a = 321 (value inside code block) 4 5 # Thanks, S.C.</PRE></TD></TR></TABLE></P><P><ANAME="BLOCKIO"></A></P><P>The code block enclosed in braces may have <AHREF="io-redirection.html#IOREDIRREF">I/O redirected</A> to and from it.</P><DIVCLASS="EXAMPLE"><HR><ANAME="EX8"></A><P><B>Example 3-1. Code blocks and I/O redirection</B></P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING"> 1 #!/bin/bash 2 # Reading lines in /etc/fstab. 3 4 File=/etc/fstab 5 6 { 7 read line1 8 read line2 9 } < $File 10 11 echo "First line in $File is:" 12 echo "$line1" 13 echo 14 echo "Second line in $File is:" 15 echo "$line2" 16 17 exit 0 18 19 # Now, how do you parse the separate fields of each line? 20 # Hint: use awk, or . . . 21 # . . . Hans-Joerg Diers suggests using the "set" Bash builtin.</PRE></TD></TR></TABLE><HR></DIV><P><ANAME="BLOCKIO2"></A></P><DIVCLASS="EXAMPLE"><HR><ANAME="RPMCHECK"></A><P><B>Example 3-2. Saving the output of a code block to a file</B></P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING"> 1 #!/bin/bash 2 # rpm-check.sh 3 4 # Queries an rpm file for description, listing, 5 #+ and whether it can be installed. 6 # Saves output to a file. 7 # 8 # This script illustrates using a code block. 9 10 SUCCESS=0 11 E_NOARGS=65 12 13 if [ -z "$1" ] 14 then 15 echo "Usage: `basename $0` rpm-file" 16 exit $E_NOARGS 17 fi 18 19 { # Begin code block. 20 echo 21 echo "Archive Description:" 22 rpm -qpi $1 # Query description. 23 echo 24 echo "Archive Listing:" 25 rpm -qpl $1 # Query listing. 26 echo 27 rpm -i --test $1 # Query whether rpm file can be installed. 28 if [ "$?" -eq $SUCCESS ] 29 then 30 echo "$1 can be installed." 31 else 32 echo "$1 cannot be installed." 33 fi 34 echo # End code block. 35 } > "$1.test" # Redirects output of everything in block to file. 36 37 echo "Results of rpm test in file $1.test" 38 39 # See rpm man page for explanation of options. 40 41 exit 0</PRE></TD></TR></TABLE><HR></DIV><DIVCLASS="NOTE"><TABLECLASS="NOTE"WIDTH="90%"BORDER="0"><TR><TDWIDTH="25"ALIGN="CENTER"VALIGN="TOP"><IMGSRC="common/note.png"HSPACE="5"ALT="Note"></TD><TDALIGN="LEFT"VALIGN="TOP"><P>Unlike a command group within (parentheses), as above, a code block enclosed by {braces} will <SPANCLASS="emphasis"><ICLASS="EMPHASIS">not</I></SPAN> normally launch a <AHREF="subshells.html#SUBSHELLSREF">subshell</A>. <ANAME="AEN1075"HREF="#FTN.AEN1075">[4]</A> </P></TD></TR></TABLE></DIV></DD><DT><SPANCLASS="TOKEN">{}</SPAN></DT><DD><DIVCLASS="FORMALPARA"><P><B>placeholder for text. </B>Used after <AHREF="moreadv.html#XARGSCURLYREF">xargs <TTCLASS="OPTION">-i</TT></A> (<ICLASS="FIRSTTERM">replace strings</I> option). The <SPANCLASS="TOKEN">{}</SPAN> double curly brackets are a placeholder for output text.</P></DIV><P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING"> 1 ls . | xargs -i -t cp ./{} $1 2 # ^^ ^^ 3 4 # From "ex42.sh" (copydir.sh) example.</PRE></TD></TR></TABLE></P></DD><DT><SPANCLASS="TOKEN">{} \;</SPAN></DT><DD><DIVCLASS="FORMALPARA"><P><B>pathname. </B>Mostly used in <AHREF="moreadv.html#FINDREF">find</A> constructs. This is <SPANCLASS="emphasis"><ICLASS="EMPHASIS">not</I></SPAN> a shell <AHREF="internal.html#BUILTINREF">builtin</A>.</P></DIV><DIVCLASS="NOTE"><TABLECLASS="NOTE"WIDTH="90%"BORDER="0"><TR><TDWIDTH="25"ALIGN="CENTER"VALIGN="TOP"><IMGSRC="common/note.png"HSPACE="5"ALT="Note"></TD><TDALIGN="LEFT"VALIGN="TOP"><P>The <SPANCLASS="QUOTE">"<SPANCLASS="TOKEN">;</SPAN>"</SPAN> ends the <TTCLASS="OPTION">-exec</TT> option of a <BCLASS="COMMAND">find</B> command sequence. It needs to be escaped to protect it from interpretation by the shell.</P></TD></TR></TABLE></DIV></DD><DT><ANAME="LEFTBRACKET"></A><SPANCLASS="TOKEN">[ ]</SPAN></DT><DD><DIVCLASS="FORMALPARA"><P><B>test. </B><ANAME="BRACKTEST"></A></P></DIV><P><AHREF="tests.html#IFTHEN">Test</A> expression between <BCLASS="COMMAND">[ ]</B>. Note that <BCLASS="COMMAND">[</B> is part of the shell <ICLASS="FIRSTTERM">builtin</I> <AHREF="tests.html#TTESTREF">test</A> (and a synonym for it), <SPANCLASS="emphasis"><ICLASS="EMPHASIS">not</I></SPAN> a link to the external command <TTCLASS="FILENAME">/usr/bin/test</TT>.</P></DD><DT><SPANCLASS="TOKEN">[[ ]]</SPAN></DT><DD><DIVCLASS="FORMALPARA"><P><B>test. </B></P></DIV><P>Test expression between <SPANCLASS="TOKEN">[[ ]]</SPAN>. This is a shell <AHREF="internal.html#KEYWORDREF">keyword</A>.</P><P>See the discussion on the <AHREF="tests.html#DBLBRACKETS">[[ ... ]] construct</A>.</P></DD><DT><SPANCLASS="TOKEN">[ ]</SPAN></DT><DD><DIVCLASS="FORMALPARA"><P><B>array element. </B></P></DIV><P>In the context of an <AHREF="arrays.html#ARRAYREF">array</A>, brackets set off the numbering of each element of that array. <TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING"> 1 Array[1]=slot_1 2 echo ${Array[1]}</PRE></TD></TR></TABLE></P></DD><DT><SPANCLASS="TOKEN">[ ]</SPAN></DT><DD><DIVCLASS="FORMALPARA"><P><B>range of characters. </B></P></DIV><P>As part of a <AHREF="regexp.html#REGEXREF">regular expression</A>, brackets delineate a <AHREF="regexp.html#BRACKETSREF">range of characters</A> to match.</P></DD><DT><SPANCLASS="TOKEN">(( ))</SPAN></DT><DD><DIVCLASS="FORMALPARA"><P><B>integer expansion. </B></P></DIV><P>Expand and evaluate integer expression between <SPANCLASS="TOKEN">(( ))</SPAN>.</P><P>See the discussion on the <AHREF="dblparens.html">(( ... )) construct</A>.</P></DD><DT><SPANCLASS="TOKEN">></SPAN> <SPANCLASS="TOKEN">&></SPAN> <SPANCLASS="TOKEN">>&</SPAN> <SPANCLASS="TOKEN">>></SPAN> <SPANCLASS="TOKEN"><</SPAN> <SPANCLASS="TOKEN"><></SPAN></DT><DD><DIVCLASS="FORMALPARA"><P><B><AHREF="io-redirection.html#IOREDIRREF">redirection</A>. </B></P></DIV><P><TTCLASS="USERINPUT"><B>scriptname >filename</B></TT> redirects the output of <TTCLASS="FILENAME">scriptname</TT> to file <TTCLASS="FILENAME">filename</TT>. Overwrite <TTCLASS="FILENAME">filename</TT> if it already exists.</P><P><ANAME="REDIROUTERROR"></A></P><P><TTCLASS="USERINPUT"><B>command &>filename</B></TT> redirects both the <AHREF="ioredirintro.html#STDINOUTDEF"><TTCLASS="FILENAME">stdout</TT></A> and the <TTCLASS="FILENAME">stderr</TT> of <TTCLASS="FILENAME">command</TT> to <TTCLASS="FILENAME">filename</TT>.</P><P><ANAME="REDIROUTERROR2"></A></P><P><TTCLASS="USERINPUT"><B>command >&2</B></TT> redirects <TTCLASS="FILENAME">stdout</TT> of <TTCLASS="FILENAME">command</TT> to <TTCLASS="FILENAME">stderr</TT>.</P><P><TTCLASS="USERINPUT"><B>scriptname >>filename</B></TT> appends the output of <TTCLASS="FILENAME">scriptname</TT> to file <TTCLASS="FILENAME">filename</TT>. If <TTCLASS="FILENAME">filename</TT> does not already exist, it is created.</P><P><ANAME="REDIRRW"></A></P><P><TTCLASS="USERINPUT"><B>[i]<>filename</B></TT> opens file <TTCLASS="FILENAME">filename</TT> for reading and writing, and assigns <AHREF="io-redirection.html#FDREF">file descriptor</A> <SPANCLASS="TOKEN">i</SPAN> to it. If <TTCLASS="FILENAME">filename</TT> does not exist, it is created.</P><DIVCLASS="FORMALPARA"><P><B><AHREF="process-sub.html#PROCESSSUBREF">process substitution</A>. </B></P></DIV><P><TTCLASS="USERINPUT"><B>(command)></B></TT></P><P><TTCLASS="USERINPUT"><B><(command)</B></TT></P><P><AHREF="comparison-ops.html#LTREF">In a different context</A>, the <SPANCLASS="QUOTE">"<SPANCLASS="TOKEN"><</SPAN>"</SPAN> and <SPANCLASS="QUOTE">"<SPANCLASS="TOKEN">></SPAN>"</SPAN> characters act as <AHREF="comparison-ops.html#SCOMPARISON1">string comparison operators</A>.</P><P><AHREF="comparison-ops.html#INTLT">In yet another context</A>,
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -