📄 bashver3.html
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><HTML><HEAD><TITLE>Bash, version 3</TITLE><METANAME="GENERATOR"CONTENT="Modular DocBook HTML Stylesheet Version 1.57"><LINKREL="HOME"TITLE="Advanced Bash-Scripting Guide"HREF="index.html"><LINKREL="UP"TITLE="Bash, versions 2 and 3"HREF="bash2.html"><LINKREL="PREVIOUS"TITLE="Bash, versions 2 and 3"HREF="bash2.html"><LINKREL="NEXT"TITLE="Endnotes"HREF="endnotes.html"><METAHTTP-EQUIV="Content-Style-Type"CONTENT="text/css"><LINKREL="stylesheet"HREF="common/kde-common.css"TYPE="text/css"><METAHTTP-EQUIV="Content-Type"CONTENT="text/html; charset=iso-8859-1"><METAHTTP-EQUIV="Content-Language"CONTENT="en"><LINKREL="stylesheet"HREF="common/kde-localised.css"TYPE="text/css"TITLE="KDE-English"><LINKREL="stylesheet"HREF="common/kde-default.css"TYPE="text/css"TITLE="KDE-Default"></HEAD><BODYCLASS="SECT1"BGCOLOR="#FFFFFF"TEXT="#000000"LINK="#AA0000"VLINK="#AA0055"ALINK="#AA0000"STYLE="font-family: sans-serif;"><DIVCLASS="NAVHEADER"><TABLEWIDTH="100%"BORDER="0"CELLPADDING="0"CELLSPACING="0"><TR><THCOLSPAN="3"ALIGN="center">Advanced Bash-Scripting Guide: An in-depth exploration of the art of shell scripting</TH></TR><TR><TDWIDTH="10%"ALIGN="left"VALIGN="bottom"><AHREF="bash2.html">Prev</A></TD><TDWIDTH="80%"ALIGN="center"VALIGN="bottom">Chapter 34. Bash, versions 2 and 3</TD><TDWIDTH="10%"ALIGN="right"VALIGN="bottom"><AHREF="endnotes.html">Next</A></TD></TR></TABLE><HRALIGN="LEFT"WIDTH="100%"></DIV><DIVCLASS="SECT1"><H1CLASS="SECT1"><ANAME="BASHVER3">34.2. Bash, version 3</A></H1><P><ANAME="BASH3REF"></A></P><P>On July 27, 2004, Chet Ramey released version 3 of Bash. This update fixes quite a number of bug in Bash and adds some new features.</P><P>Some of the added features are: <UL><LI><P>A new, more generalized <BCLASS="COMMAND">{a..z}</B> <AHREF="special-chars.html#BRACEEXPREF">brace expansion</A> operator. <TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING"> 1 #!/bin/bash 2 3 for i in {1..10} 4 # Simpler and more straightforward than 5 #+ for i in $(seq 10) 6 do 7 echo -n "$i " 8 done 9 10 echo 11 12 # 1 2 3 4 5 6 7 8 9 10</PRE></TD></TR></TABLE> </P></LI><LI><P>The <BCLASS="COMMAND">${!array[@]}</B> operator, which expands to all the indices of a given <AHREF="arrays.html#ARRAYREF">array</A>. <TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING"> 1 #!/bin/bash 2 3 Array=(element-zero element-one element-two element-three) 4 5 echo ${Array[0]} # element-zero 6 # First element of array. 7 8 echo ${!Array[@]} # 0 1 2 3 9 # All the indices of Array. 10 11 for i in ${!Array[@]} 12 do 13 echo ${Array[i]} # element-zero 14 # element-one 15 # element-two 16 # element-three 17 # 18 # All the elements in Array. 19 done</PRE></TD></TR></TABLE> </P></LI><LI><P><ANAME="REGEXMATCHREF"></A></P><P>The <BCLASS="COMMAND">=~</B> <AHREF="regexp.html#REGEXREF">Regular Expression</A> matching operator within a <AHREF="tests.html#DBLBRACKETS">double brackets</A> test expression. (Perl has a similar operator.)</P><P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING"> 1 #!/bin/bash 2 3 variable="This is a fine mess." 4 5 echo "$variable" 6 7 if [[ "$variable" =~ "T*fin*es*" ]] 8 # Regex matching with =~ operator within [[ double brackets ]]. 9 then 10 echo "match found" 11 # match found 12 fi</PRE></TD></TR></TABLE></P><P>Or, more usefully:</P><P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING"> 1 #!/bin/bash 2 3 input=$1 4 5 6 if [[ "$input" =~ "[1-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9][0-9][0-9]" ]] 7 # NNN-NN-NNNN 8 # Where each N is a digit. 9 # But, initial digit must not be 0. 10 then 11 echo "Social Security number." 12 # Process SSN. 13 else 14 echo "Not a Social Security number!" 15 # Or, ask for corrected input. 16 fi</PRE></TD></TR></TABLE></P><P>For another example of using the <BCLASS="COMMAND">=~</B> operator, see <AHREF="contributed-scripts.html#WHX">Example A-28</A>.</P></LI></UL> </P><DIVCLASS="CAUTION"><TABLECLASS="CAUTION"WIDTH="100%"BORDER="0"><TR><TDWIDTH="25"ALIGN="CENTER"VALIGN="TOP"><IMGSRC="common/caution.png"HSPACE="5"ALT="Caution"></TD><TDALIGN="LEFT"VALIGN="TOP"><P>The update to version 3 of Bash breaks a few scripts that worked under earlier versions. <ICLASS="EMPHASIS">Test critical legacy scripts to make sure they still work!</I></P><P>As it happens, a couple of the scripts in the <ICLASS="EMPHASIS">Advanced Bash Scripting Guide</I> had to be fixed up (see <AHREF="contributed-scripts.html#OBJORIENTED">Example A-20</A> and <AHREF="variables2.html#TOUT">Example 9-4</A>, for instance).</P></TD></TR></TABLE></DIV></DIV><DIVCLASS="NAVFOOTER"><HRALIGN="LEFT"WIDTH="100%"><TABLEWIDTH="100%"BORDER="0"CELLPADDING="0"CELLSPACING="0"><TR><TDWIDTH="33%"ALIGN="left"VALIGN="top"><AHREF="bash2.html">Prev</A></TD><TDWIDTH="34%"ALIGN="center"VALIGN="top"><AHREF="index.html">Home</A></TD><TDWIDTH="33%"ALIGN="right"VALIGN="top"><AHREF="endnotes.html">Next</A></TD></TR><TR><TDWIDTH="33%"ALIGN="left"VALIGN="top">Bash, versions 2 and 3</TD><TDWIDTH="34%"ALIGN="center"VALIGN="top"><AHREF="bash2.html">Up</A></TD><TDWIDTH="33%"ALIGN="right"VALIGN="top">Endnotes</TD></TR></TABLE></DIV></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -