📄 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.76b+"><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"><TABLESUMMARY="Header navigation table"WIDTH="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"ACCESSKEY="P">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"ACCESSKEY="N">Next</A></TD></TR></TABLE><HRALIGN="LEFT"WIDTH="100%"></DIV><DIVCLASS="SECT1"><H1CLASS="SECT1"><ANAME="BASHVER3"></A>34.2. Bash, version 3</H1><P><ANAME="BASH3REF"></A></P><P>On July 27, 2004, Chet Ramey released version 3 of Bash. This update fixed quite a number of bugs and added new features.</P><P>Some of the more important added features: <UL><LI><P><ANAME="BRACEEXPREF3"></A></P><P>A new, more generalized <BCLASS="COMMAND">{a..z}</B> <AHREF="special-chars.html#BRACEEXPREF">brace expansion</A> operator.</P><P><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 13 14 15 16 # Or just . . . 17 18 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 19 echo {z..a} # z y x w v u t s r q p o n m l k j i h g f e d c b a 20 # Works backwards, too. 21 echo {3..-2} # 3 2 1 0 -1 -2 22 echo {X..d} # X Y Z [ ] ^ _ ` a b c d 23 # Shows (some of) the ASCII characters between Z and a, 24 #+ but don't rely on this type of behavior because . . . 25 echo {]..a} # {]..a} 26 # Why?</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>.</P><P><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 # Regex matching with =~ operator within [[ double brackets ]]. 8 if [[ "$variable" =~ "T.........fin*es*" ]] 9 # ^ ^ 10 # NOTE: Quoting not necessary, as of version 3.2 of Bash. 11 then 12 echo "match found" 13 # match found 14 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 # ^ NOTE: Quoting not necessary, as of version 3.2 of Bash. 8 # NNN-NN-NNNN (where each N is a digit). Initial digit must not be 0. 9 then 10 echo "Social Security number." 11 # Process SSN. 12 else 13 echo "Not a Social Security number!" 14 # Or, ask for corrected input. 15 fi</PRE></TD></TR></TABLE></P><P>For additional examples of using the <BCLASS="COMMAND">=~</B> operator, see <AHREF="contributed-scripts.html#WHX">Example A-31</A>, <AHREF="here-docs.html#MAILBOXGREP">Example 18-14</A>, <AHREF="contributed-scripts.html#FINDSPLIT">Example A-37</A>, and <AHREF="contributed-scripts.html#TOHTML">Example A-26</A>.</P></LI><LI><P><ANAME="PIPEFAILREF"></A></P><P>The new <TTCLASS="OPTION">set -o pipefail</TT> option is useful for debugging <AHREF="special-chars.html#PIPEREF">pipes</A>. If this option is set, then the <AHREF="exit-status.html#EXITSTATUSREF">exit status</A> of a pipe is the exit status of the last command in the pipe to <SPANCLASS="emphasis"><ICLASS="EMPHASIS">fail</I></SPAN> (return a non-zero value), rather than the actual final command in the pipe.</P><P>See <AHREF="communications.html#FC4UPD">Example 15-42</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. <SPANCLASS="emphasis"><ICLASS="EMPHASIS">Test critical legacy scripts to make sure they still work!</I></SPAN></P><P>As it happens, a couple of the scripts in the <SPANCLASS="emphasis"><ICLASS="EMPHASIS">Advanced Bash Scripting Guide</I></SPAN> had to be fixed up (see <AHREF="contributed-scripts.html#OBJORIENTED">Example A-21</A> and <AHREF="variables2.html#TOUT">Example 9-4</A>, for instance).</P></TD></TR></TABLE></DIV><DIVCLASS="SECT2"><H2CLASS="SECT2"><ANAME="AEN19311"></A>34.2.1. Bash, version 3.1</H2><P>The version 3.1 update of Bash introduces a number of bugfixes and a few minor changes.</P><UL><LI><P>The <SPANCLASS="TOKEN">+=</SPAN> operator is now permitted in in places where previously only the <SPANCLASS="TOKEN">=</SPAN> assignment operator was recognized.</P><P><ANAME="PLUSEQSTR"></A></P><P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING"> 1 a=1 2 echo $a # 1 3 4 a+=5 # Won't work under versions of Bash earlier than 3.1. 5 echo $a # 15 6 7 a+=Hello 8 echo $a # 15Hello</PRE></TD></TR></TABLE> </P><P>Here, <SPANCLASS="TOKEN">+=</SPAN> functions as a <ICLASS="FIRSTTERM">string concatenation</I> operator. Note that its behavior in this particular context is different than within a <AHREF="internal.html#LETREF">let</A> construct.</P><P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING"> 1 a=1 2 echo $a # 1 3 4 let a+=5 # Integer arithmetic, rather than string concatenation. 5 echo $a # 6 6 7 let a+=Hello # Doesn't "add" anything to a. 8 echo $a # 6</PRE></TD></TR></TABLE> </P></LI></UL></DIV><DIVCLASS="SECT2"><H2CLASS="SECT2"><ANAME="AEN19329"></A>34.2.2. Bash, version 3.2</H2><P>This is pretty much a bugfix update.</P><UL><LI><P>In <AHREF="parameter-substitution.html#PSGLOB"><ICLASS="FIRSTTERM">global</I> parameter substitutions</A>, the pattern no longer anchors at the start of the string.</P></LI><LI><P>The <TTCLASS="OPTION">--wordexp</TT> option disables <AHREF="process-sub.html#PROCESSSUBREF">process substitution</A>.</P></LI><LI><P>The <BCLASS="COMMAND">=~</B> <AHREF="bashver3.html#REGEXMATCHREF">Regular Expression match operator</A> no longer requires <AHREF="quoting.html#QUOTINGREF">quoting</A> of the <ICLASS="FIRSTTERM">pattern</I> within <AHREF="tests.html#DBLBRACKETS">[[ ... ]]</A>.</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>In fact, quoting in this context is <SPANCLASS="emphasis"><ICLASS="EMPHASIS">not</I></SPAN> advisable as it may cause <ICLASS="FIRSTTERM">regex</I> evaluation to fail. See the <AHREF="https://bugs.launchpad.net/ubuntu-website/+bug/109931"TARGET="_top"> Ubuntu Bug List</A> and <AHREF="http://en.wikinerds.org/index.php/Bash_syntax_and_semantics"TARGET="_top"> Wikinerds on Bash syntax</A>.</P><P>With Bash version 3.2.25(1), running on Fedora Core, quoting works, but do not assume this will be the case on <SPANCLASS="emphasis"><ICLASS="EMPHASIS">your</I></SPAN> machine.</P></TD></TR></TABLE></DIV></LI></UL></DIV></DIV><DIVCLASS="NAVFOOTER"><HRALIGN="LEFT"WIDTH="100%"><TABLESUMMARY="Footer navigation table"WIDTH="100%"BORDER="0"CELLPADDING="0"CELLSPACING="0"><TR><TDWIDTH="33%"ALIGN="left"VALIGN="top"><AHREF="bash2.html"ACCESSKEY="P">Prev</A></TD><TDWIDTH="34%"ALIGN="center"VALIGN="top"><AHREF="index.html"ACCESSKEY="H">Home</A></TD><TDWIDTH="33%"ALIGN="right"VALIGN="top"><AHREF="endnotes.html"ACCESSKEY="N">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"ACCESSKEY="U">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 + -