Debian Codegraph @

HTML/varassignment.html

raw symbols SCIP
1 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
2 <HTML
3 ><HEAD
4 ><TITLE
5 >Variable Assignment</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="Introduction to Variables and Parameters"
15 HREF="variables.html"><LINK
16 REL="PREVIOUS"
17 TITLE="Introduction to Variables and Parameters"
18 HREF="variables.html"><LINK
19 REL="NEXT"
20 TITLE="Bash Variables Are Untyped"
21 HREF="untyped.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="SECT1"
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="variables.html"
68 ACCESSKEY="P"
69 >Prev</A
70 ></TD
71 ><TD
72 WIDTH="80%"
73 ALIGN="center"
74 VALIGN="bottom"
75 >Chapter 4. Introduction to Variables and Parameters</TD
76 ><TD
77 WIDTH="10%"
78 ALIGN="right"
79 VALIGN="bottom"
80 ><A
81 HREF="untyped.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="SECT1"
92 ><H1
93 CLASS="SECT1"
94 ><A
95 NAME="VARASSIGNMENT"
96 ></A
97 >4.2. Variable Assignment</H1
98 ><DIV
99 CLASS="VARIABLELIST"
100 ><DL
101 ><DT
102 ><A
103 NAME="EQREF"
104 ></A
105 ><SPAN
106 CLASS="TOKEN"
107 >=</SPAN
108 ></DT
109 ><DD
110 ><P
111 >the assignment operator (<SPAN
112 CLASS="emphasis"
113 ><I
114 CLASS="EMPHASIS"
115 >no space before
116 and after</I
117 ></SPAN
118 >)</P
119 ><DIV
120 CLASS="CAUTION"
121 ><TABLE
122 CLASS="CAUTION"
123 WIDTH="90%"
124 BORDER="0"
125 ><TR
126 ><TD
127 WIDTH="25"
128 ALIGN="CENTER"
129 VALIGN="TOP"
130 ><IMG
131 SRC="common/caution.png"
132 HSPACE="5"
133 ALT="Caution"></TD
134 ><TD
135 ALIGN="LEFT"
136 VALIGN="TOP"
137 ><P
138 >Do not confuse this with <A
139 HREF="comparison-ops.html#EQUALSIGNREF"
140 >=</A
141 > and
142 <A
143 HREF="comparison-ops.html#EQUALREF"
144 >-eq</A
145 >, which
146 <A
147 HREF="tests.html#IFTHEN"
148 >test</A
149 >,
150 rather than assign!</P
151 ><P
152 >Note that <SPAN
153 CLASS="TOKEN"
154 >=</SPAN
155 > can be either
156 an <I
157 CLASS="FIRSTTERM"
158 >assignment</I
159 > or a
160 <I
161 CLASS="FIRSTTERM"
162 >test</I
163 > operator, depending on
164 context.</P
165 ></TD
166 ></TR
167 ></TABLE
168 ></DIV
169 ><P
170 ><A
171 NAME="EX15_0"
172 ></A
173 ></P
174 ><DIV
175 CLASS="EXAMPLE"
176 ><HR><A
177 NAME="EX15"
178 ></A
179 ><P
180 ><B
181 >Example 4-2. Plain Variable Assignment</B
182 ></P
183 ><TABLE
184 BORDER="0"
185 BGCOLOR="#E0E0E0"
186 WIDTH="90%"
187 ><TR
188 ><TD
189 ><PRE
190 CLASS="PROGRAMLISTING"
191 > 1&nbsp;#!/bin/bash
192 2&nbsp;# Naked variables
193 3&nbsp;
194 4&nbsp;echo
195 5&nbsp;
196 6&nbsp;# When is a variable "naked", i.e., lacking the '$' in front?
197 7&nbsp;# When it is being assigned, rather than referenced.
198 8&nbsp;
199 9&nbsp;# Assignment
200 10&nbsp;a=879
201 11&nbsp;echo "The value of \"a\" is $a."
202 12&nbsp;
203 13&nbsp;# Assignment using 'let'
204 14&nbsp;let a=16+5
205 15&nbsp;echo "The value of \"a\" is now $a."
206 16&nbsp;
207 17&nbsp;echo
208 18&nbsp;
209 19&nbsp;# In a 'for' loop (really, a type of disguised assignment):
210 20&nbsp;echo -n "Values of \"a\" in the loop are: "
211 21&nbsp;for a in 7 8 9 11
212 22&nbsp;do
213 23&nbsp; echo -n "$a "
214 24&nbsp;done
215 25&nbsp;
216 26&nbsp;echo
217 27&nbsp;echo
218 28&nbsp;
219 29&nbsp;# In a 'read' statement (also a type of assignment):
220 30&nbsp;echo -n "Enter \"a\" "
221 31&nbsp;read a
222 32&nbsp;echo "The value of \"a\" is now $a."
223 33&nbsp;
224 34&nbsp;echo
225 35&nbsp;
226 36&nbsp;exit 0</PRE
227 ></TD
228 ></TR
229 ></TABLE
230 ><HR></DIV
231 ><P
232 ><A
233 NAME="EX16_0"
234 ></A
235 ></P
236 ><DIV
237 CLASS="EXAMPLE"
238 ><HR><A
239 NAME="EX16"
240 ></A
241 ><P
242 ><B
243 >Example 4-3. Variable Assignment, plain and fancy</B
244 ></P
245 ><TABLE
246 BORDER="0"
247 BGCOLOR="#E0E0E0"
248 WIDTH="90%"
249 ><TR
250 ><TD
251 ><PRE
252 CLASS="PROGRAMLISTING"
253 > 1&nbsp;#!/bin/bash
254 2&nbsp;
255 3&nbsp;a=23 # Simple case
256 4&nbsp;echo $a
257 5&nbsp;b=$a
258 6&nbsp;echo $b
259 7&nbsp;
260 8&nbsp;# Now, getting a little bit fancier (command substitution).
261 9&nbsp;
262 10&nbsp;a=`echo Hello!` # Assigns result of 'echo' command to 'a' ...
263 11&nbsp;echo $a
264 12&nbsp;# Note that including an exclamation mark (!) within a
265 13&nbsp;#+ command substitution construct will not work from the command-line,
266 14&nbsp;#+ since this triggers the Bash "history mechanism."
267 15&nbsp;# Inside a script, however, the history functions are disabled by default.
268 16&nbsp;
269 17&nbsp;a=`ls -l` # Assigns result of 'ls -l' command to 'a'
270 18&nbsp;echo $a # Unquoted, however, it removes tabs and newlines.
271 19&nbsp;echo
272 20&nbsp;echo "$a" # The quoted variable preserves whitespace.
273 21&nbsp; # (See the chapter on "Quoting.")
274 22&nbsp;
275 23&nbsp;exit 0</PRE
276 ></TD
277 ></TR
278 ></TABLE
279 ><HR></DIV
280 ><P
281 ><A
282 NAME="COMMANDSUBREF0"
283 ></A
284 ></P
285 ><P
286 >Variable assignment using the <I
287 CLASS="FIRSTTERM"
288 >$(...)</I
289 >
290 mechanism (a newer method than <A
291 HREF="commandsub.html#BACKQUOTESREF"
292 >backquotes</A
293 >). This is
294 likewise a form of <A
295 HREF="commandsub.html#COMMANDSUBREF"
296 >command
297 substitution</A
298 >.</P
299 ><P
300 ><TABLE
301 BORDER="0"
302 BGCOLOR="#E0E0E0"
303 WIDTH="90%"
304 ><TR
305 ><TD
306 ><PRE
307 CLASS="PROGRAMLISTING"
308 > 1&nbsp;# From /etc/rc.d/rc.local
309 2&nbsp;R=$(cat /etc/redhat-release)
310 3&nbsp;arch=$(uname -m)</PRE
311 ></TD
312 ></TR
313 ></TABLE
314 ></P
315 ></DD
316 ></DL
317 ></DIV
318 ></DIV
319 ><DIV
320 CLASS="NAVFOOTER"
321 ><HR
322 ALIGN="LEFT"
323 WIDTH="100%"><TABLE
324 SUMMARY="Footer navigation table"
325 WIDTH="100%"
326 BORDER="0"
327 CELLPADDING="0"
328 CELLSPACING="0"
329 ><TR
330 ><TD
331 WIDTH="33%"
332 ALIGN="left"
333 VALIGN="top"
334 ><A
335 HREF="variables.html"
336 ACCESSKEY="P"
337 >Prev</A
338 ></TD
339 ><TD
340 WIDTH="34%"
341 ALIGN="center"
342 VALIGN="top"
343 ><A
344 HREF="index.html"
345 ACCESSKEY="H"
346 >Home</A
347 ></TD
348 ><TD
349 WIDTH="33%"
350 ALIGN="right"
351 VALIGN="top"
352 ><A
353 HREF="untyped.html"
354 ACCESSKEY="N"
355 >Next</A
356 ></TD
357 ></TR
358 ><TR
359 ><TD
360 WIDTH="33%"
361 ALIGN="left"
362 VALIGN="top"
363 >Introduction to Variables and Parameters</TD
364 ><TD
365 WIDTH="34%"
366 ALIGN="center"
367 VALIGN="top"
368 ><A
369 HREF="variables.html"
370 ACCESSKEY="U"
371 >Up</A
372 ></TD
373 ><TD
374 WIDTH="33%"
375 ALIGN="right"
376 VALIGN="top"
377 >Bash Variables Are Untyped</TD
378 ></TR
379 ></TABLE
380 ></DIV
381 ></BODY
382 ></HTML
383 >