- •Advanced Bash-Scripting Guide
- •Dedication
- •Table of Contents
- •Part 1. Introduction
- •Advanced Bash-Scripting Guide
- •Chapter 2. Starting Off With a Sha-Bang
- •2.1. Invoking the script
- •2.2. Preliminary Exercises
- •Part 2. Basics
- •Chapter 3. Exit and Exit Status
- •Chapter 4. Special Characters
- •Chapter 5. Introduction to Variables and Parameters
- •5.1. Variable Substitution
- •5.2. Variable Assignment
- •5.3. Bash Variables Are Untyped
- •5.4. Special Variable Types
- •Chapter 6. Quoting
- •Chapter 7. Tests
- •7.1. Test Constructs
- •7.2. File test operators
- •7.3. Comparison operators (binary)
- •7.4. Nested if/then Condition Tests
- •7.5. Testing Your Knowledge of Tests
- •8.1. Operators
- •8.2. Numerical Constants
- •Part 3. Beyond the Basics
- •Chapter 9. Variables Revisited
- •9.1. Internal Variables
- •9.2. Manipulating Strings
- •9.3. Parameter Substitution
- •9.4. Typing variables: declare or typeset
- •9.5. Indirect References to Variables
- •9.6. $RANDOM: generate random integer
- •9.7. The Double Parentheses Construct
- •Chapter 10. Loops and Branches
- •10.1. Loops
- •10.2. Nested Loops
- •10.3. Loop Control
- •10.4. Testing and Branching
- •Chapter 11. Internal Commands and Builtins
- •12.1. Basic Commands
- •12.2. Complex Commands
- •12.3. Time / Date Commands
- •12.4. Text Processing Commands
- •12.5. File and Archiving Commands
- •12.6. Communications Commands
- •12.7. Terminal Control Commands
- •12.8. Math Commands
- •12.9. Miscellaneous Commands
- •Chapter 13. System and Administrative Commands
- •Chapter 14. Command Substitution
- •Chapter 15. Arithmetic Expansion
- •Chapter 16. I/O Redirection
- •16.1. Using exec
- •16.2. Redirecting Code Blocks
- •16.3. Applications
- •Chapter 17. Here Documents
- •Chapter 18. Recess Time
- •Part 4. Advanced Topics
- •Chapter 19. Regular Expressions
- •19.1. A Brief Introduction to Regular Expressions
- •19.2. Globbing
- •Chapter 20. Subshells
- •Chapter 21. Restricted Shells
- •Chapter 22. Process Substitution
- •Chapter 23. Functions
- •23.1. Complex Functions and Function Complexities
- •23.2. Local Variables
- •Chapter 24. Aliases
- •Chapter 25. List Constructs
- •Chapter 26. Arrays
- •Chapter 27. Files
- •Chapter 28. /dev and /proc
- •28.2. /proc
- •Chapter 29. Of Zeros and Nulls
- •Chapter 30. Debugging
- •Chapter 31. Options
- •Chapter 32. Gotchas
- •Chapter 33. Scripting With Style
- •Chapter 34. Miscellany
- •34.2. Shell Wrappers
- •34.3. Tests and Comparisons: Alternatives
- •34.4. Optimizations
- •34.5. Assorted Tips
- •34.6. Oddities
- •34.7. Security Issues
- •34.8. Portability Issues
- •34.9. Shell Scripting Under Windows
- •Chapter 35. Bash, version 2
- •36. Endnotes
- •36.1. Author's Note
- •36.2. About the Author
- •36.3. Tools Used to Produce This Book
- •36.4. Credits
- •List of Tables
- •List of Examples
- •Bibliography
Loops and Branches
|
Advanced Bash-Scripting Guide: |
Prev |
Next |
Chapter 10. Loops and Branches
Table of Contents
10.1.Loops
10.2.Nested Loops
10.3.Loop Control
10.4.Testing and Branching
Operations on code blocks are the key to structured, organized shell scripts. Looping and branching constructs provide the tools for accomplishing this.
Prev |
Home |
Next |
The Double Parentheses |
Up |
Loops |
Construct |
|
|
http://tldp.org/LDP/abs/html/loops.html [7/15/2002 6:34:01 PM]
Variable Substitution
|
Advanced Bash-Scripting Guide: |
|
Prev |
Chapter 5. Introduction to Variables and Parameters |
Next |
5.1. Variable Substitution
The name of a variable is a placeholder for its value, the data it holds. Referencing its value is called variable substitution.
$
Let us carefully distinguish between the name of a variable and its value. If variable1 is the name of a variable, then $variable1 is a reference to its value, the data item it contains. The only time a variable appears "naked", without the $ prefix, is when declared or assigned, when unset, when exported, or in the special case of a variable representing a signal (see Example 30-5). Assignment may be with an = (as in var1=27), in a read statement, and at the head of a loop (for var2 in 1 2 3).
Enclosing a referenced value in double quotes (" ") does not interfere with variable substitution. This is called partial quoting, sometimes referred to as "weak quoting". Using single quotes (' ') causes the variable name to be used literally, and no substitution will take place. This is full quoting, sometimes referred to as "strong quoting". See Chapter 6 for a detailed discussion.
Note that $variable is actually a simplified alternate form of ${variable}. In contexts where the $variable syntax causes an error, the longer form may work (see Section 9.3, below).
Example 5-1. Variable assignment and substitution
#!/bin/bash
# Variables: assignment and substitution
a=375 hello=$a
#-------------------------------------------------------------------------
#No space permitted on either side of = sign when initializing variables.
#If "VARIABLE =value",
#+ script tries to run "VARIABLE" command with one argument, "=value".
#If "VARIABLE= value",
#+ script tries |
to run "value" command with |
|
#+ the environmental variable "VARIABLE" set to "". |
||
#------------------------------------------------------------------------- |
|
|
echo hello |
# |
Not a variable reference, just the string "hello". |
http://tldp.org/LDP/abs/html/varsubn.html (1 of 3) [7/15/2002 6:34:01 PM]
Variable Substitution
echo $hello
echo ${hello} # Identical to above.
echo "$hello" echo "${hello}"
echo |
|
|
|
hello="A B C |
|
D" |
|
echo $hello |
# |
A B C D |
|
echo "$hello" |
# |
A B C |
D |
# As you see, |
echo $hello |
and echo "$hello" give different results. |
# Quoting a variable preserves whitespace.
echo
echo '$hello' # $hello
#Variable referencing disabled by single quotes, #+ which causes the "$" to be interpreted literally.
#Notice the effect of different types of quoting.
hello= |
# Setting it to |
a null value. |
|
echo "\$hello (null value) = $hello" |
|||
# |
Note that setting a variable to a null value is not the same as |
||
#+ |
unsetting it, although |
the end result is the same (see below). |
# --------------------------------------------------------------
#It is permissible to set multiple variables on the same line, #+ if separated by white space.
#Caution, this may reduce legibility, and may not be portable.
var1=variable1 var2=variable2 var3=variable3 echo
echo "var1=$var1 var2=$var2 var3=$var3"
# May cause problems with older versions of "sh".
# --------------------------------------------------------------
echo; echo
numbers="one two three" other_numbers="1 2 3"
# If whitespace within a variable, then quotes necessary. echo "numbers = $numbers"
echo "other_numbers = $other_numbers" # other_numbers = 1 2 3 echo
echo "uninitialized_variable = $uninitialized_variable"
http://tldp.org/LDP/abs/html/varsubn.html (2 of 3) [7/15/2002 6:34:01 PM]
Variable Substitution
# Uninitialized variable has null value (no value at all). uninitialized_variable= # Declaring, but not initializing it
#+ (same as setting it to a null value, as above). echo "uninitialized_variable = $uninitialized_variable"
# It still has a null value.
uninitialized_variable=23 |
# |
Set it. |
unset uninitialized_variable |
# |
Unset it. |
echo "uninitialized_variable = $uninitialized_variable"
# It still has a null value.
echo
exit 0
An uninitialized variable has a "null" value - no assigned value at all (not zero!). Using a variable before assigning a value to it will inevitably cause problems.
Prev |
Home |
Next |
Introduction to Variables and |
Up |
Variable Assignment |
Parameters |
|
|
http://tldp.org/LDP/abs/html/varsubn.html (3 of 3) [7/15/2002 6:34:01 PM]