Debian Codegraph @

HTML/operations.html

raw symbols SCIP
1 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
2 <HTML
3 ><HEAD
4 ><TITLE
5 >Operations and Related Topics</TITLE
6 ><META
7 NAME="GENERATOR"
8 CONTENT="Modular DocBook HTML Stylesheet Version 1.76b+
9 "><LINK
10 REL="HOME"
11 TITLE="Advanced Bash-Scripting Guide"
12 HREF="index.html"><LINK
13 REL="UP"
14 TITLE="Basics"
15 HREF="part2.html"><LINK
16 REL="PREVIOUS"
17 TITLE="Testing Your Knowledge of Tests"
18 HREF="testtest.html"><LINK
19 REL="NEXT"
20 TITLE="Numerical Constants"
21 HREF="numerical-constants.html"><META
22 HTTP-EQUIV="Content-Style-Type"
23 CONTENT="text/css"><LINK
24 REL="stylesheet"
25 HREF="common/kde-common.css"
26 TYPE="text/css"><META
27 HTTP-EQUIV="Content-Type"
28 CONTENT="text/html; charset=iso-8859-1"><META
29 HTTP-EQUIV="Content-Language"
30 CONTENT="en"><LINK
31 REL="stylesheet"
32 HREF="common/kde-localised.css"
33 TYPE="text/css"
34 TITLE="KDE-English"><LINK
35 REL="stylesheet"
36 HREF="common/kde-default.css"
37 TYPE="text/css"
38 TITLE="KDE-Default"></HEAD
39 ><BODY
40 CLASS="CHAPTER"
41 BGCOLOR="#FFFFFF"
42 TEXT="#000000"
43 LINK="#AA0000"
44 VLINK="#AA0055"
45 ALINK="#AA0000"
46 STYLE="font-family: sans-serif;"
47 ><DIV
48 CLASS="NAVHEADER"
49 ><TABLE
50 SUMMARY="Header navigation table"
51 WIDTH="100%"
52 BORDER="0"
53 CELLPADDING="0"
54 CELLSPACING="0"
55 ><TR
56 ><TH
57 COLSPAN="3"
58 ALIGN="center"
59 >Advanced Bash-Scripting Guide: An in-depth exploration of the art of shell scripting</TH
60 ></TR
61 ><TR
62 ><TD
63 WIDTH="10%"
64 ALIGN="left"
65 VALIGN="bottom"
66 ><A
67 HREF="testtest.html"
68 ACCESSKEY="P"
69 >Prev</A
70 ></TD
71 ><TD
72 WIDTH="80%"
73 ALIGN="center"
74 VALIGN="bottom"
75 ></TD
76 ><TD
77 WIDTH="10%"
78 ALIGN="right"
79 VALIGN="bottom"
80 ><A
81 HREF="numerical-constants.html"
82 ACCESSKEY="N"
83 >Next</A
84 ></TD
85 ></TR
86 ></TABLE
87 ><HR
88 ALIGN="LEFT"
89 WIDTH="100%"></DIV
90 ><DIV
91 CLASS="CHAPTER"
92 ><H1
93 ><A
94 NAME="OPERATIONS"
95 ></A
96 >Chapter 8. Operations and Related Topics</H1
97 ><DIV
98 CLASS="SECT1"
99 ><H1
100 CLASS="SECT1"
101 ><A
102 NAME="OPS"
103 ></A
104 >8.1. Operators</H1
105 ><DIV
106 CLASS="VARIABLELIST"
107 ><P
108 ><B
109 ><A
110 NAME="ASNOP1"
111 ></A
112 >assignment</B
113 ></P
114 ><DL
115 ><DT
116 ><TT
117 CLASS="REPLACEABLE"
118 ><I
119 >variable assignment</I
120 ></TT
121 ></DT
122 ><DD
123 ><P
124 >Initializing or changing the value of a variable</P
125 ></DD
126 ><DT
127 >=</DT
128 ><DD
129 ><P
130 >All-purpose assignment operator, which works for both
131 arithmetic and string assignments.</P
132 ><P
133 > <TABLE
134 BORDER="0"
135 BGCOLOR="#E0E0E0"
136 WIDTH="90%"
137 ><TR
138 ><TD
139 ><PRE
140 CLASS="PROGRAMLISTING"
141 > 1&nbsp;var=27
142 2&nbsp;category=minerals # No spaces allowed after the "=".</PRE
143 ></TD
144 ></TR
145 ></TABLE
146 >
147 </P
148 ><DIV
149 CLASS="CAUTION"
150 ><TABLE
151 CLASS="CAUTION"
152 WIDTH="90%"
153 BORDER="0"
154 ><TR
155 ><TD
156 WIDTH="25"
157 ALIGN="CENTER"
158 VALIGN="TOP"
159 ><IMG
160 SRC="common/caution.png"
161 HSPACE="5"
162 ALT="Caution"></TD
163 ><TD
164 ALIGN="LEFT"
165 VALIGN="TOP"
166 ><P
167 >Do not confuse the <SPAN
168 CLASS="QUOTE"
169 >"="</SPAN
170 > assignment
171 operator with the <A
172 HREF="comparison-ops.html#EQUALSIGNREF"
173 >= test
174 operator</A
175 >.</P
176 ><P
177 > <TABLE
178 BORDER="0"
179 BGCOLOR="#E0E0E0"
180 WIDTH="90%"
181 ><TR
182 ><TD
183 ><PRE
184 CLASS="PROGRAMLISTING"
185 > 1&nbsp;# = as a test operator
186 2&nbsp;
187 3&nbsp;if [ "$string1" = "$string2" ]
188 4&nbsp;then
189 5&nbsp; command
190 6&nbsp;fi
191 7&nbsp;
192 8&nbsp;# if [ "X$string1" = "X$string2" ] is safer,
193 9&nbsp;#+ to prevent an error message should one of the variables be empty.
194 10&nbsp;# (The prepended "X" characters cancel out.)</PRE
195 ></TD
196 ></TR
197 ></TABLE
198 >
199 </P
200 ></TD
201 ></TR
202 ></TABLE
203 ></DIV
204 ></DD
205 ></DL
206 ></DIV
207 ><DIV
208 CLASS="VARIABLELIST"
209 ><P
210 ><B
211 ><A
212 NAME="AROPS1"
213 ></A
214 >arithmetic operators</B
215 ></P
216 ><DL
217 ><DT
218 ><SPAN
219 CLASS="TOKEN"
220 >+</SPAN
221 ></DT
222 ><DD
223 ><P
224 >plus</P
225 ></DD
226 ><DT
227 ><SPAN
228 CLASS="TOKEN"
229 >-</SPAN
230 ></DT
231 ><DD
232 ><P
233 >minus</P
234 ></DD
235 ><DT
236 ><SPAN
237 CLASS="TOKEN"
238 >*</SPAN
239 ></DT
240 ><DD
241 ><P
242 >multiplication</P
243 ></DD
244 ><DT
245 ><SPAN
246 CLASS="TOKEN"
247 >/</SPAN
248 ></DT
249 ><DD
250 ><P
251 >division</P
252 ></DD
253 ><DT
254 ><A
255 NAME="EXPONENTIATIONREF"
256 ></A
257 ><SPAN
258 CLASS="TOKEN"
259 >**</SPAN
260 ></DT
261 ><DD
262 ><P
263 >exponentiation</P
264 ><P
265 > <TABLE
266 BORDER="0"
267 BGCOLOR="#E0E0E0"
268 WIDTH="90%"
269 ><TR
270 ><TD
271 ><PRE
272 CLASS="PROGRAMLISTING"
273 > 1&nbsp;# Bash, version 2.02, introduced the "**" exponentiation operator.
274 2&nbsp;
275 3&nbsp;let "z=5**3" # 5 * 5 * 5
276 4&nbsp;echo "z = $z" # z = 125</PRE
277 ></TD
278 ></TR
279 ></TABLE
280 >
281 </P
282 ></DD
283 ><DT
284 ><A
285 NAME="MODULOREF"
286 ></A
287 ><SPAN
288 CLASS="TOKEN"
289 >%</SPAN
290 ></DT
291 ><DD
292 ><P
293 >modulo, or mod (returns the
294 <I
295 CLASS="FIRSTTERM"
296 >remainder</I
297 > of an integer division
298 operation)</P
299 ><P
300 > <TABLE
301 BORDER="0"
302 BGCOLOR="#E0E0E0"
303 WIDTH="90%"
304 ><TR
305 ><TD
306 ><PRE
307 CLASS="SCREEN"
308 > <TT
309 CLASS="PROMPT"
310 >bash$ </TT
311 ><TT
312 CLASS="USERINPUT"
313 ><B
314 >expr 5 % 3</B
315 ></TT
316 >
317 <TT
318 CLASS="COMPUTEROUTPUT"
319 >2</TT
320 >
321 </PRE
322 ></TD
323 ></TR
324 ></TABLE
325 >
326 <SPAN
327 CLASS="emphasis"
328 ><I
329 CLASS="EMPHASIS"
330 >5/3 = 1, with remainder 2</I
331 ></SPAN
332 >
333 </P
334 ><P
335 >This operator finds use in, among other things,
336 generating numbers within a specific range (see <A
337 HREF="randomvar.html#EX21"
338 >Example 9-11</A
339 > and <A
340 HREF="randomvar.html#RANDOMTEST"
341 >Example 9-15</A
342 >) and
343 formatting program output (see <A
344 HREF="arrays.html#QFUNCTION"
345 >Example 27-16</A
346 > and
347 <A
348 HREF="contributed-scripts.html#COLLATZ"
349 >Example A-6</A
350 >). It can even be used to generate
351 prime numbers, (see <A
352 HREF="contributed-scripts.html#PRIMES"
353 >Example A-15</A
354 >). Modulo turns
355 up surprisingly often in numerical recipes.</P
356 ><DIV
357 CLASS="EXAMPLE"
358 ><HR><A
359 NAME="GCD"
360 ></A
361 ><P
362 ><B
363 >Example 8-1. Greatest common divisor</B
364 ></P
365 ><TABLE
366 BORDER="0"
367 BGCOLOR="#E0E0E0"
368 WIDTH="90%"
369 ><TR
370 ><TD
371 ><PRE
372 CLASS="PROGRAMLISTING"
373 > 1&nbsp;#!/bin/bash
374 2&nbsp;# gcd.sh: greatest common divisor
375 3&nbsp;# Uses Euclid's algorithm
376 4&nbsp;
377 5&nbsp;# The "greatest common divisor" (gcd) of two integers
378 6&nbsp;#+ is the largest integer that will divide both, leaving no remainder.
379 7&nbsp;
380 8&nbsp;# Euclid's algorithm uses successive division.
381 9&nbsp;# In each pass,
382 10&nbsp;#+ dividend &#60;--- divisor
383 11&nbsp;#+ divisor &#60;--- remainder
384 12&nbsp;#+ until remainder = 0.
385 13&nbsp;# The gcd = dividend, on the final pass.
386 14&nbsp;#
387 15&nbsp;# For an excellent discussion of Euclid's algorithm, see
388 16&nbsp;#+ Jim Loy's site, http://www.jimloy.com/number/euclids.htm.
389 17&nbsp;
390 18&nbsp;
391 19&nbsp;# ------------------------------------------------------
392 20&nbsp;# Argument check
393 21&nbsp;ARGS=2
394 22&nbsp;E_BADARGS=85
395 23&nbsp;
396 24&nbsp;if [ $# -ne "$ARGS" ]
397 25&nbsp;then
398 26&nbsp; echo "Usage: `basename $0` first-number second-number"
399 27&nbsp; exit $E_BADARGS
400 28&nbsp;fi
401 29&nbsp;# ------------------------------------------------------
402 30&nbsp;
403 31&nbsp;
404 32&nbsp;gcd ()
405 33&nbsp;{
406 34&nbsp;
407 35&nbsp; dividend=$1 # Arbitrary assignment.
408 36&nbsp; divisor=$2 #! It doesn't matter which of the two is larger.
409 37&nbsp; # Why not?
410 38&nbsp;
411 39&nbsp; remainder=1 # If an uninitialized variable is used inside
412 40&nbsp; #+ test brackets, an error message results.
413 41&nbsp;
414 42&nbsp; until [ "$remainder" -eq 0 ]
415 43&nbsp; do # ^^^^^^^^^^ Must be previously initialized!
416 44&nbsp; let "remainder = $dividend % $divisor"
417 45&nbsp; dividend=$divisor # Now repeat with 2 smallest numbers.
418 46&nbsp; divisor=$remainder
419 47&nbsp; done # Euclid's algorithm
420 48&nbsp;
421 49&nbsp;} # Last $dividend is the gcd.
422 50&nbsp;
423 51&nbsp;
424 52&nbsp;gcd $1 $2
425 53&nbsp;
426 54&nbsp;echo; echo "GCD of $1 and $2 = $dividend"; echo
427 55&nbsp;
428 56&nbsp;
429 57&nbsp;# Exercises :
430 58&nbsp;# ---------
431 59&nbsp;# 1) Check command-line arguments to make sure they are integers,
432 60&nbsp;#+ and exit the script with an appropriate error message if not.
433 61&nbsp;# 2) Rewrite the gcd () function to use local variables.
434 62&nbsp;
435 63&nbsp;exit 0</PRE
436 ></TD
437 ></TR
438 ></TABLE
439 ><HR></DIV
440 ></DD
441 ><DT
442 ><A
443 NAME="ARITHOPSCOMB"
444 ></A
445 ><SPAN
446 CLASS="TOKEN"
447 >+=</SPAN
448 ></DT
449 ><DD
450 ><P
451 ><I
452 CLASS="FIRSTTERM"
453 >plus-equal</I
454 > (increment variable
455 by a constant)
456
457 <A
458 NAME="AEN3907"
459 HREF="#FTN.AEN3907"
460 >[1]</A
461 >
462
463 </P
464 ><P
465 ><TT
466 CLASS="USERINPUT"
467 ><B
468 >let "var += 5"</B
469 ></TT
470 > results in
471 <TT
472 CLASS="PARAMETER"
473 ><I
474 >var</I
475 ></TT
476 > being incremented by
477 <TT
478 CLASS="LITERAL"
479 >5</TT
480 >.</P
481 ></DD
482 ><DT
483 ><SPAN
484 CLASS="TOKEN"
485 >-=</SPAN
486 ></DT
487 ><DD
488 ><P
489 ><I
490 CLASS="FIRSTTERM"
491 >minus-equal</I
492 > (decrement
493 variable by a constant)</P
494 ></DD
495 ><DT
496 ><SPAN
497 CLASS="TOKEN"
498 >*=</SPAN
499 ></DT
500 ><DD
501 ><P
502 ><I
503 CLASS="FIRSTTERM"
504 >times-equal</I
505 > (multiply
506 variable by a constant)</P
507 ><P
508 ><TT
509 CLASS="USERINPUT"
510 ><B
511 >let "var *= 4"</B
512 ></TT
513 > results in <TT
514 CLASS="PARAMETER"
515 ><I
516 >var</I
517 ></TT
518 >
519 being multiplied by <TT
520 CLASS="LITERAL"
521 >4</TT
522 >.</P
523 ></DD
524 ><DT
525 ><SPAN
526 CLASS="TOKEN"
527 >/=</SPAN
528 ></DT
529 ><DD
530 ><P
531 ><I
532 CLASS="FIRSTTERM"
533 >slash-equal</I
534 > (divide
535 variable by a constant)</P
536 ></DD
537 ><DT
538 ><SPAN
539 CLASS="TOKEN"
540 >%=</SPAN
541 ></DT
542 ><DD
543 ><P
544 ><I
545 CLASS="FIRSTTERM"
546 >mod-equal</I
547 >
548 (<I
549 CLASS="FIRSTTERM"
550 >remainder</I
551 >
552 of dividing variable by a constant)</P
553 ><P
554 ><SPAN
555 CLASS="emphasis"
556 ><I
557 CLASS="EMPHASIS"
558 >Arithmetic operators often occur in an
559 <A
560 HREF="moreadv.html#EXPRREF"
561 >expr</A
562 > or <A
563 HREF="internal.html#LETREF"
564 >let</A
565 > expression.</I
566 ></SPAN
567 ></P
568 ><DIV
569 CLASS="EXAMPLE"
570 ><HR><A
571 NAME="ARITHOPS"
572 ></A
573 ><P
574 ><B
575 >Example 8-2. Using Arithmetic Operations</B
576 ></P
577 ><TABLE
578 BORDER="0"
579 BGCOLOR="#E0E0E0"
580 WIDTH="90%"
581 ><TR
582 ><TD
583 ><PRE
584 CLASS="PROGRAMLISTING"
585 > 1&nbsp;#!/bin/bash
586 2&nbsp;# Counting to 11 in 10 different ways.
587 3&nbsp;
588 4&nbsp;n=1; echo -n "$n "
589 5&nbsp;
590 6&nbsp;let "n = $n + 1" # let "n = n + 1" also works.
591 7&nbsp;echo -n "$n "
592 8&nbsp;
593 9&nbsp;
594 10&nbsp;: $((n = $n + 1))
595 11&nbsp;# ":" necessary because otherwise Bash attempts
596 12&nbsp;#+ to interpret "$((n = $n + 1))" as a command.
597 13&nbsp;echo -n "$n "
598 14&nbsp;
599 15&nbsp;(( n = n + 1 ))
600 16&nbsp;# A simpler alternative to the method above.
601 17&nbsp;# Thanks, David Lombard, for pointing this out.
602 18&nbsp;echo -n "$n "
603 19&nbsp;
604 20&nbsp;n=$(($n + 1))
605 21&nbsp;echo -n "$n "
606 22&nbsp;
607 23&nbsp;: $[ n = $n + 1 ]
608 24&nbsp;# ":" necessary because otherwise Bash attempts
609 25&nbsp;#+ to interpret "$[ n = $n + 1 ]" as a command.
610 26&nbsp;# Works even if "n" was initialized as a string.
611 27&nbsp;echo -n "$n "
612 28&nbsp;
613 29&nbsp;n=$[ $n + 1 ]
614 30&nbsp;# Works even if "n" was initialized as a string.
615 31&nbsp;#* Avoid this type of construct, since it is obsolete and nonportable.
616 32&nbsp;# Thanks, Stephane Chazelas.
617 33&nbsp;echo -n "$n "
618 34&nbsp;
619 35&nbsp;# Now for C-style increment operators.
620 36&nbsp;# Thanks, Frank Wang, for pointing this out.
621 37&nbsp;
622 38&nbsp;let "n++" # let "++n" also works.
623 39&nbsp;echo -n "$n "
624 40&nbsp;
625 41&nbsp;(( n++ )) # (( ++n )) also works.
626 42&nbsp;echo -n "$n "
627 43&nbsp;
628 44&nbsp;: $(( n++ )) # : $(( ++n )) also works.
629 45&nbsp;echo -n "$n "
630 46&nbsp;
631 47&nbsp;: $[ n++ ] # : $[ ++n ] also works
632 48&nbsp;echo -n "$n "
633 49&nbsp;
634 50&nbsp;echo
635 51&nbsp;
636 52&nbsp;exit 0</PRE
637 ></TD
638 ></TR
639 ></TABLE
640 ><HR></DIV
641 ></DD
642 ></DL
643 ></DIV
644 ><P
645 ><A
646 NAME="INTVARREF"
647 ></A
648 ></P
649 ><DIV
650 CLASS="NOTE"
651 ><TABLE
652 CLASS="NOTE"
653 WIDTH="100%"
654 BORDER="0"
655 ><TR
656 ><TD
657 WIDTH="25"
658 ALIGN="CENTER"
659 VALIGN="TOP"
660 ><IMG
661 SRC="common/note.png"
662 HSPACE="5"
663 ALT="Note"></TD
664 ><TD
665 ALIGN="LEFT"
666 VALIGN="TOP"
667 ><P
668 >Integer variables in older versions of Bash were signed
669 <I
670 CLASS="FIRSTTERM"
671 >long</I
672 > (32-bit) integers, in the range of
673 -2147483648 to 2147483647. An operation that took a variable
674 outside these limits gave an erroneous result.</P
675 ><P
676 > <TABLE
677 BORDER="0"
678 BGCOLOR="#E0E0E0"
679 WIDTH="100%"
680 ><TR
681 ><TD
682 ><PRE
683 CLASS="PROGRAMLISTING"
684 > 1&nbsp;echo $BASH_VERSION # 1.14
685 2&nbsp;
686 3&nbsp;a=2147483646
687 4&nbsp;echo "a = $a" # a = 2147483646
688 5&nbsp;let "a+=1" # Increment "a".
689 6&nbsp;echo "a = $a" # a = 2147483647
690 7&nbsp;let "a+=1" # increment "a" again, past the limit.
691 8&nbsp;echo "a = $a" # a = -2147483648
692 9&nbsp; # ERROR: out of range,
693 10&nbsp; # + and the leftmost bit, the sign bit,
694 11&nbsp; # + has been set, making the result negative.</PRE
695 ></TD
696 ></TR
697 ></TABLE
698 >
699 </P
700 ><P
701 >As of version &#62;= 2.05b, Bash supports 64-bit integers.</P
702 ></TD
703 ></TR
704 ></TABLE
705 ></DIV
706 ><DIV
707 CLASS="CAUTION"
708 ><TABLE
709 CLASS="CAUTION"
710 WIDTH="100%"
711 BORDER="0"
712 ><TR
713 ><TD
714 WIDTH="25"
715 ALIGN="CENTER"
716 VALIGN="TOP"
717 ><IMG
718 SRC="common/caution.png"
719 HSPACE="5"
720 ALT="Caution"></TD
721 ><TD
722 ALIGN="LEFT"
723 VALIGN="TOP"
724 ><P
725 ><A
726 NAME="NOFLOATINGPOINT"
727 ></A
728 ></P
729 ><P
730 >Bash does not understand floating point arithmetic. It
731 treats numbers containing a decimal point as strings.</P
732 ><P
733 > <TABLE
734 BORDER="0"
735 BGCOLOR="#E0E0E0"
736 WIDTH="100%"
737 ><TR
738 ><TD
739 ><PRE
740 CLASS="PROGRAMLISTING"
741 > 1&nbsp;a=1.5
742 2&nbsp;
743 3&nbsp;let "b = $a + 1.3" # Error.
744 4&nbsp;# t2.sh: let: b = 1.5 + 1.3: syntax error in expression
745 5&nbsp;# (error token is ".5 + 1.3")
746 6&nbsp;
747 7&nbsp;echo "b = $b" # b=1</PRE
748 ></TD
749 ></TR
750 ></TABLE
751 >
752 </P
753 ><P
754 >Use <A
755 HREF="mathc.html#BCREF"
756 >bc</A
757 > in scripts that that need floating
758 point calculations or math library functions.</P
759 ></TD
760 ></TR
761 ></TABLE
762 ></DIV
763 ><DIV
764 CLASS="FORMALPARA"
765 ><P
766 ><B
767 >bitwise operators. </B
768 >The bitwise operators seldom make an appearance in shell scripts.
769 Their chief use seems to be manipulating and testing values read
770 from ports or <A
771 HREF="devproc.html#SOCKETREF"
772 >sockets</A
773 >. <SPAN
774 CLASS="QUOTE"
775 >"Bit
776 flipping"</SPAN
777 > is more relevant to compiled languages, such
778 as C and C++, which provide direct access to system
779 hardware. However, see <SPAN
780 CLASS="emphasis"
781 ><I
782 CLASS="EMPHASIS"
783 >vladz's</I
784 ></SPAN
785 >
786 ingenious use of bitwise operators in his
787 <I
788 CLASS="FIRSTTERM"
789 >base64.sh</I
790 > (<A
791 HREF="contributed-scripts.html#BASE64"
792 >Example A-54</A
793 >)
794 script. </P
795 ></DIV
796 ><DIV
797 CLASS="VARIABLELIST"
798 ><P
799 ><B
800 ><A
801 NAME="BITWSOPS1"
802 ></A
803 >bitwise operators</B
804 ></P
805 ><DL
806 ><DT
807 ><SPAN
808 CLASS="TOKEN"
809 >&#60;&#60;</SPAN
810 ></DT
811 ><DD
812 ><P
813 >bitwise left shift (multiplies by <TT
814 CLASS="LITERAL"
815 >2</TT
816 >
817 for each shift position)</P
818 ></DD
819 ><DT
820 ><SPAN
821 CLASS="TOKEN"
822 >&#60;&#60;=</SPAN
823 ></DT
824 ><DD
825 ><P
826 ><I
827 CLASS="FIRSTTERM"
828 >left-shift-equal</I
829 ></P
830 ><P
831 ><TT
832 CLASS="USERINPUT"
833 ><B
834 >let "var &#60;&#60;= 2"</B
835 ></TT
836 > results in <TT
837 CLASS="PARAMETER"
838 ><I
839 >var</I
840 ></TT
841 >
842 left-shifted <TT
843 CLASS="LITERAL"
844 >2</TT
845 > bits (multiplied by <TT
846 CLASS="LITERAL"
847 >4</TT
848 >)</P
849 ></DD
850 ><DT
851 ><SPAN
852 CLASS="TOKEN"
853 >&#62;&#62;</SPAN
854 ></DT
855 ><DD
856 ><P
857 >bitwise right shift (divides by <TT
858 CLASS="LITERAL"
859 >2</TT
860 >
861 for each shift position)</P
862 ></DD
863 ><DT
864 ><SPAN
865 CLASS="TOKEN"
866 >&#62;&#62;=</SPAN
867 ></DT
868 ><DD
869 ><P
870 ><I
871 CLASS="FIRSTTERM"
872 >right-shift-equal</I
873 >
874 (inverse of <SPAN
875 CLASS="TOKEN"
876 >&#60;&#60;=</SPAN
877 >)</P
878 ></DD
879 ><DT
880 ><SPAN
881 CLASS="TOKEN"
882 >&#38;</SPAN
883 ></DT
884 ><DD
885 ><P
886 >bitwise AND</P
887 ></DD
888 ><DT
889 ><SPAN
890 CLASS="TOKEN"
891 >&#38;=</SPAN
892 ></DT
893 ><DD
894 ><P
895 >bitwise <I
896 CLASS="FIRSTTERM"
897 >AND-equal</I
898 ></P
899 ></DD
900 ><DT
901 ><SPAN
902 CLASS="TOKEN"
903 >|</SPAN
904 ></DT
905 ><DD
906 ><P
907 >bitwise OR</P
908 ></DD
909 ><DT
910 ><SPAN
911 CLASS="TOKEN"
912 >|=</SPAN
913 ></DT
914 ><DD
915 ><P
916 >bitwise <I
917 CLASS="FIRSTTERM"
918 >OR-equal</I
919 ></P
920 ></DD
921 ><DT
922 ><SPAN
923 CLASS="TOKEN"
924 >~</SPAN
925 ></DT
926 ><DD
927 ><P
928 >bitwise NOT</P
929 ></DD
930 ><DT
931 ><SPAN
932 CLASS="TOKEN"
933 >^</SPAN
934 ></DT
935 ><DD
936 ><P
937 >bitwise XOR</P
938 ></DD
939 ><DT
940 ><SPAN
941 CLASS="TOKEN"
942 >^=</SPAN
943 ></DT
944 ><DD
945 ><P
946 >bitwise <I
947 CLASS="FIRSTTERM"
948 >XOR-equal</I
949 ></P
950 ></DD
951 ></DL
952 ></DIV
953 ><DIV
954 CLASS="VARIABLELIST"
955 ><P
956 ><B
957 ><A
958 NAME="LOGOPS1"
959 ></A
960 >logical (boolean) operators</B
961 ></P
962 ><DL
963 ><DT
964 ><SPAN
965 CLASS="TOKEN"
966 >!</SPAN
967 ></DT
968 ><DD
969 ><P
970 >NOT</P
971 ><P
972 ><TABLE
973 BORDER="0"
974 BGCOLOR="#E0E0E0"
975 WIDTH="90%"
976 ><TR
977 ><TD
978 ><PRE
979 CLASS="PROGRAMLISTING"
980 > 1&nbsp;if [ ! -f $FILENAME ]
981 2&nbsp;then
982 3&nbsp; ...</PRE
983 ></TD
984 ></TR
985 ></TABLE
986 ></P
987 ></DD
988 ><DT
989 ><SPAN
990 CLASS="TOKEN"
991 >&#38;&#38;</SPAN
992 ></DT
993 ><DD
994 ><P
995 >AND</P
996 ><P
997 ><TABLE
998 BORDER="0"
999 BGCOLOR="#E0E0E0"
1000 WIDTH="90%"
1001 ><TR
1002 ><TD
1003 ><PRE
1004 CLASS="PROGRAMLISTING"
1005 > 1&nbsp;if [ $condition1 ] &#38;&#38; [ $condition2 ]
1006 2&nbsp;# Same as: if [ $condition1 -a $condition2 ]
1007 3&nbsp;# Returns true if both condition1 and condition2 hold true...
1008 4&nbsp;
1009 5&nbsp;if [[ $condition1 &#38;&#38; $condition2 ]] # Also works.
1010 6&nbsp;# Note that &#38;&#38; operator not permitted <SPAN
1011 CLASS="emphasis"
1012 ><I
1013 CLASS="EMPHASIS"
1014 >inside brackets</I
1015 ></SPAN
1016 >
1017 7&nbsp;#+ of [ ... ] construct.</PRE
1018 ></TD
1019 ></TR
1020 ></TABLE
1021 ></P
1022 ><DIV
1023 CLASS="NOTE"
1024 ><TABLE
1025 CLASS="NOTE"
1026 WIDTH="90%"
1027 BORDER="0"
1028 ><TR
1029 ><TD
1030 WIDTH="25"
1031 ALIGN="CENTER"
1032 VALIGN="TOP"
1033 ><IMG
1034 SRC="common/note.png"
1035 HSPACE="5"
1036 ALT="Note"></TD
1037 ><TD
1038 ALIGN="LEFT"
1039 VALIGN="TOP"
1040 ><P
1041 ><SPAN
1042 CLASS="TOKEN"
1043 >&#38;&#38;</SPAN
1044 > may also be used, depending on context,
1045 in an <A
1046 HREF="list-cons.html#LISTCONSREF"
1047 >and list</A
1048 >
1049 to concatenate commands.</P
1050 ></TD
1051 ></TR
1052 ></TABLE
1053 ></DIV
1054 ></DD
1055 ><DT
1056 ><A
1057 NAME="ORREF"
1058 ></A
1059 ><SPAN
1060 CLASS="TOKEN"
1061 >||</SPAN
1062 ></DT
1063 ><DD
1064 ><P
1065 >OR</P
1066 ><P
1067 ><TABLE
1068 BORDER="0"
1069 BGCOLOR="#E0E0E0"
1070 WIDTH="90%"
1071 ><TR
1072 ><TD
1073 ><PRE
1074 CLASS="PROGRAMLISTING"
1075 > 1&nbsp;if [ $condition1 ] || [ $condition2 ]
1076 2&nbsp;# Same as: if [ $condition1 -o $condition2 ]
1077 3&nbsp;# Returns true if either condition1 or condition2 holds true...
1078 4&nbsp;
1079 5&nbsp;if [[ $condition1 || $condition2 ]] # Also works.
1080 6&nbsp;# Note that || operator not permitted <SPAN
1081 CLASS="emphasis"
1082 ><I
1083 CLASS="EMPHASIS"
1084 >inside brackets</I
1085 ></SPAN
1086 >
1087 7&nbsp;#+ of a [ ... ] construct.</PRE
1088 ></TD
1089 ></TR
1090 ></TABLE
1091 ></P
1092 ><DIV
1093 CLASS="NOTE"
1094 ><TABLE
1095 CLASS="NOTE"
1096 WIDTH="90%"
1097 BORDER="0"
1098 ><TR
1099 ><TD
1100 WIDTH="25"
1101 ALIGN="CENTER"
1102 VALIGN="TOP"
1103 ><IMG
1104 SRC="common/note.png"
1105 HSPACE="5"
1106 ALT="Note"></TD
1107 ><TD
1108 ALIGN="LEFT"
1109 VALIGN="TOP"
1110 ><P
1111 >Bash tests the <A
1112 HREF="exit-status.html#EXITSTATUSREF"
1113 >exit
1114 status</A
1115 > of each statement linked with a logical
1116 operator.</P
1117 ></TD
1118 ></TR
1119 ></TABLE
1120 ></DIV
1121 ><DIV
1122 CLASS="EXAMPLE"
1123 ><HR><A
1124 NAME="ANDOR"
1125 ></A
1126 ><P
1127 ><B
1128 >Example 8-3. Compound Condition Tests Using &#38;&#38; and ||</B
1129 ></P
1130 ><TABLE
1131 BORDER="0"
1132 BGCOLOR="#E0E0E0"
1133 WIDTH="90%"
1134 ><TR
1135 ><TD
1136 ><PRE
1137 CLASS="PROGRAMLISTING"
1138 > 1&nbsp;#!/bin/bash
1139 2&nbsp;
1140 3&nbsp;a=24
1141 4&nbsp;b=47
1142 5&nbsp;
1143 6&nbsp;if [ "$a" -eq 24 ] &#38;&#38; [ "$b" -eq 47 ]
1144 7&nbsp;then
1145 8&nbsp; echo "Test #1 succeeds."
1146 9&nbsp;else
1147 10&nbsp; echo "Test #1 fails."
1148 11&nbsp;fi
1149 12&nbsp;
1150 13&nbsp;# ERROR: if [ "$a" -eq 24 &#38;&#38; "$b" -eq 47 ]
1151 14&nbsp;#+ attempts to execute ' [ "$a" -eq 24 '
1152 15&nbsp;#+ and fails to finding matching ']'.
1153 16&nbsp;#
1154 17&nbsp;# Note: if [[ $a -eq 24 &#38;&#38; $b -eq 24 ]] works.
1155 18&nbsp;# The double-bracket if-test is more flexible
1156 19&nbsp;#+ than the single-bracket version.
1157 20&nbsp;# (The "&#38;&#38;" has a different meaning in line 17 than in line 6.)
1158 21&nbsp;# Thanks, Stephane Chazelas, for pointing this out.
1159 22&nbsp;
1160 23&nbsp;
1161 24&nbsp;if [ "$a" -eq 98 ] || [ "$b" -eq 47 ]
1162 25&nbsp;then
1163 26&nbsp; echo "Test #2 succeeds."
1164 27&nbsp;else
1165 28&nbsp; echo "Test #2 fails."
1166 29&nbsp;fi
1167 30&nbsp;
1168 31&nbsp;
1169 32&nbsp;# The -a and -o options provide
1170 33&nbsp;#+ an alternative compound condition test.
1171 34&nbsp;# Thanks to Patrick Callahan for pointing this out.
1172 35&nbsp;
1173 36&nbsp;
1174 37&nbsp;if [ "$a" -eq 24 -a "$b" -eq 47 ]
1175 38&nbsp;then
1176 39&nbsp; echo "Test #3 succeeds."
1177 40&nbsp;else
1178 41&nbsp; echo "Test #3 fails."
1179 42&nbsp;fi
1180 43&nbsp;
1181 44&nbsp;
1182 45&nbsp;if [ "$a" -eq 98 -o "$b" -eq 47 ]
1183 46&nbsp;then
1184 47&nbsp; echo "Test #4 succeeds."
1185 48&nbsp;else
1186 49&nbsp; echo "Test #4 fails."
1187 50&nbsp;fi
1188 51&nbsp;
1189 52&nbsp;
1190 53&nbsp;a=rhino
1191 54&nbsp;b=crocodile
1192 55&nbsp;if [ "$a" = rhino ] &#38;&#38; [ "$b" = crocodile ]
1193 56&nbsp;then
1194 57&nbsp; echo "Test #5 succeeds."
1195 58&nbsp;else
1196 59&nbsp; echo "Test #5 fails."
1197 60&nbsp;fi
1198 61&nbsp;
1199 62&nbsp;exit 0</PRE
1200 ></TD
1201 ></TR
1202 ></TABLE
1203 ><HR></DIV
1204 ><P
1205 >The <SPAN
1206 CLASS="TOKEN"
1207 >&#38;&#38;</SPAN
1208 > and <SPAN
1209 CLASS="TOKEN"
1210 >||</SPAN
1211 > operators also
1212 find use in an arithmetic context.</P
1213 ><P
1214 > <TABLE
1215 BORDER="0"
1216 BGCOLOR="#E0E0E0"
1217 WIDTH="90%"
1218 ><TR
1219 ><TD
1220 ><PRE
1221 CLASS="SCREEN"
1222 > <TT
1223 CLASS="PROMPT"
1224 >bash$ </TT
1225 ><TT
1226 CLASS="USERINPUT"
1227 ><B
1228 >echo $(( 1 &#38;&#38; 2 )) $((3 &#38;&#38; 0)) $((4 || 0)) $((0 || 0))</B
1229 ></TT
1230 >
1231 <TT
1232 CLASS="COMPUTEROUTPUT"
1233 >1 0 1 0</TT
1234 >
1235 </PRE
1236 ></TD
1237 ></TR
1238 ></TABLE
1239 >
1240 </P
1241 ></DD
1242 ></DL
1243 ></DIV
1244 ><DIV
1245 CLASS="VARIABLELIST"
1246 ><P
1247 ><B
1248 ><A
1249 NAME="MISCOP1"
1250 ></A
1251 >miscellaneous operators</B
1252 ></P
1253 ><DL
1254 ><DT
1255 ><A
1256 NAME="COMMAOP"
1257 ></A
1258 ><SPAN
1259 CLASS="TOKEN"
1260 >,</SPAN
1261 ></DT
1262 ><DD
1263 ><P
1264 >Comma operator</P
1265 ><P
1266 >The <B
1267 CLASS="COMMAND"
1268 >comma operator</B
1269 > chains together
1270 two or more arithmetic operations. All the operations are
1271 evaluated (with possible <I
1272 CLASS="FIRSTTERM"
1273 >side
1274 effects</I
1275 >.
1276 <A
1277 NAME="AEN4242"
1278 HREF="#FTN.AEN4242"
1279 >[2]</A
1280 >
1281 </P
1282 ><P
1283 > <TABLE
1284 BORDER="0"
1285 BGCOLOR="#E0E0E0"
1286 WIDTH="90%"
1287 ><TR
1288 ><TD
1289 ><PRE
1290 CLASS="PROGRAMLISTING"
1291 > 1&nbsp;let "t1 = ((5 + 3, 7 - 1, 15 - 4))"
1292 2&nbsp;echo "t1 = $t1" ^^^^^^ # t1 = 11
1293 3&nbsp;# Here t1 is set to the result of the last operation. Why?
1294 4&nbsp;
1295 5&nbsp;let "t2 = ((a = 9, 15 / 3))" # Set "a" and calculate "t2".
1296 6&nbsp;echo "t2 = $t2 a = $a" # t2 = 5 a = 9</PRE
1297 ></TD
1298 ></TR
1299 ></TABLE
1300 >
1301 </P
1302 ><P
1303 >The comma operator finds use mainly in <A
1304 HREF="loops.html#FORLOOPREF1"
1305 >for loops</A
1306 >. See <A
1307 HREF="loops.html#FORLOOPC"
1308 >Example 11-13</A
1309 >.</P
1310 ></DD
1311 ></DL
1312 ></DIV
1313 ></DIV
1314 ></DIV
1315 ><H3
1316 CLASS="FOOTNOTES"
1317 >Notes</H3
1318 ><TABLE
1319 BORDER="0"
1320 CLASS="FOOTNOTES"
1321 WIDTH="100%"
1322 ><TR
1323 ><TD
1324 ALIGN="LEFT"
1325 VALIGN="TOP"
1326 WIDTH="5%"
1327 ><A
1328 NAME="FTN.AEN3907"
1329 HREF="operations.html#AEN3907"
1330 >[1]</A
1331 ></TD
1332 ><TD
1333 ALIGN="LEFT"
1334 VALIGN="TOP"
1335 WIDTH="95%"
1336 ><P
1337 >In a different context, <B
1338 CLASS="COMMAND"
1339 >+=</B
1340 > can
1341 serve as a <I
1342 CLASS="FIRSTTERM"
1343 >string concatenation</I
1344 >
1345 operator. This can be useful for <A
1346 HREF="bashver3.html#PATHAPPEND"
1347 >modifying <I
1348 CLASS="FIRSTTERM"
1349 >environmental
1350 variables</I
1351 ></A
1352 >.</P
1353 ></TD
1354 ></TR
1355 ><TR
1356 ><TD
1357 ALIGN="LEFT"
1358 VALIGN="TOP"
1359 WIDTH="5%"
1360 ><A
1361 NAME="FTN.AEN4242"
1362 HREF="operations.html#AEN4242"
1363 >[2]</A
1364 ></TD
1365 ><TD
1366 ALIGN="LEFT"
1367 VALIGN="TOP"
1368 WIDTH="95%"
1369 ><P
1370 ><I
1371 CLASS="FIRSTTERM"
1372 >Side effects</I
1373 >
1374 are, of course, unintended -- and usually undesirable --
1375 consequences.</P
1376 ></TD
1377 ></TR
1378 ></TABLE
1379 ><DIV
1380 CLASS="NAVFOOTER"
1381 ><HR
1382 ALIGN="LEFT"
1383 WIDTH="100%"><TABLE
1384 SUMMARY="Footer navigation table"
1385 WIDTH="100%"
1386 BORDER="0"
1387 CELLPADDING="0"
1388 CELLSPACING="0"
1389 ><TR
1390 ><TD
1391 WIDTH="33%"
1392 ALIGN="left"
1393 VALIGN="top"
1394 ><A
1395 HREF="testtest.html"
1396 ACCESSKEY="P"
1397 >Prev</A
1398 ></TD
1399 ><TD
1400 WIDTH="34%"
1401 ALIGN="center"
1402 VALIGN="top"
1403 ><A
1404 HREF="index.html"
1405 ACCESSKEY="H"
1406 >Home</A
1407 ></TD
1408 ><TD
1409 WIDTH="33%"
1410 ALIGN="right"
1411 VALIGN="top"
1412 ><A
1413 HREF="numerical-constants.html"
1414 ACCESSKEY="N"
1415 >Next</A
1416 ></TD
1417 ></TR
1418 ><TR
1419 ><TD
1420 WIDTH="33%"
1421 ALIGN="left"
1422 VALIGN="top"
1423 >Testing Your Knowledge of Tests</TD
1424 ><TD
1425 WIDTH="34%"
1426 ALIGN="center"
1427 VALIGN="top"
1428 ><A
1429 HREF="part2.html"
1430 ACCESSKEY="U"
1431 >Up</A
1432 ></TD
1433 ><TD
1434 WIDTH="33%"
1435 ALIGN="right"
1436 VALIGN="top"
1437 >Numerical Constants</TD
1438 ></TR
1439 ></TABLE
1440 ></DIV
1441 ></BODY
1442 ></HTML
1443 >