📄 dblparens.html
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><HTML><HEAD><TITLE>The Double Parentheses Construct</TITLE><METANAME="GENERATOR"CONTENT="Modular DocBook HTML Stylesheet Version 1.57"><LINKREL="HOME"TITLE="Advanced Bash-Scripting Guide"HREF="index.html"><LINKREL="UP"TITLE="Variables Revisited"HREF="variables2.html"><LINKREL="PREVIOUS"TITLE="$RANDOM: generate random integer"HREF="randomvar.html"><LINKREL="NEXT"TITLE="Loops and Branches"HREF="loops.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="randomvar.html">Prev</A></TD><TDWIDTH="80%"ALIGN="center"VALIGN="bottom">Chapter 9. Variables Revisited</TD><TDWIDTH="10%"ALIGN="right"VALIGN="bottom"><AHREF="loops.html">Next</A></TD></TR></TABLE><HRALIGN="LEFT"WIDTH="100%"></DIV><DIVCLASS="SECT1"><H1CLASS="SECT1"><ANAME="DBLPARENS">9.7. The Double Parentheses Construct</A></H1><P>Similar to the <AHREF="internal.html#LETREF">let</A> command, the <BCLASS="COMMAND">((...))</B> construct permits arithmetic expansion and evaluation. In its simplest form, <TTCLASS="USERINPUT"><B>a=$(( 5 + 3 ))</B></TT> would set <SPANCLASS="QUOTE">"a"</SPAN> to <SPANCLASS="QUOTE">"5 + 3"</SPAN>, or 8. However, this double parentheses construct is also a mechanism for allowing C-type manipulation of variables in Bash.</P><DIVCLASS="EXAMPLE"><HR><ANAME="CVARS"></A><P><B>Example 9-30. C-type manipulation of variables</B></P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="PROGRAMLISTING"> 1 #!/bin/bash 2 # Manipulating a variable, C-style, using the ((...)) construct. 3 4 5 echo 6 7 (( a = 23 )) # Setting a value, C-style, with spaces on both sides of the "=". 8 echo "a (initial value) = $a" 9 10 (( a++ )) # Post-increment 'a', C-style. 11 echo "a (after a++) = $a" 12 13 (( a-- )) # Post-decrement 'a', C-style. 14 echo "a (after a--) = $a" 15 16 17 (( ++a )) # Pre-increment 'a', C-style. 18 echo "a (after ++a) = $a" 19 20 (( --a )) # Pre-decrement 'a', C-style. 21 echo "a (after --a) = $a" 22 23 echo 24 25 ######################################################## 26 # Note that, as in C, pre- and post-decrement operators 27 #+ have slightly different side-effects. 28 29 n=1; let --n && echo "True" || echo "False" # False 30 n=1; let n-- && echo "True" || echo "False" # True 31 32 # Thanks, Jeroen Domburg. 33 ######################################################## 34 35 echo 36 37 (( t = a<45?7:11 )) # C-style trinary operator. 38 echo "If a < 45, then t = 7, else t = 11." 39 echo "t = $t " # Yes! 40 41 echo 42 43 44 # ----------------- 45 # Easter Egg alert! 46 # ----------------- 47 # Chet Ramey apparently snuck a bunch of undocumented C-style constructs 48 #+ into Bash (actually adapted from ksh, pretty much). 49 # In the Bash docs, Ramey calls ((...)) shell arithmetic, 50 #+ but it goes far beyond that. 51 # Sorry, Chet, the secret is now out. 52 53 # See also "for" and "while" loops using the ((...)) construct. 54 55 # These work only with Bash, version 2.04 or later. 56 57 exit 0</PRE></TD></TR></TABLE><HR></DIV><P>See also <AHREF="loops.html#FORLOOPC">Example 10-12</A>.</P></DIV><DIVCLASS="NAVFOOTER"><HRALIGN="LEFT"WIDTH="100%"><TABLEWIDTH="100%"BORDER="0"CELLPADDING="0"CELLSPACING="0"><TR><TDWIDTH="33%"ALIGN="left"VALIGN="top"><AHREF="randomvar.html">Prev</A></TD><TDWIDTH="34%"ALIGN="center"VALIGN="top"><AHREF="index.html">Home</A></TD><TDWIDTH="33%"ALIGN="right"VALIGN="top"><AHREF="loops.html">Next</A></TD></TR><TR><TDWIDTH="33%"ALIGN="left"VALIGN="top">$RANDOM: generate random integer</TD><TDWIDTH="34%"ALIGN="center"VALIGN="top"><AHREF="variables2.html">Up</A></TD><TDWIDTH="33%"ALIGN="right"VALIGN="top">Loops and Branches</TD></TR></TABLE></DIV></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -