Advanced Bash-Scripting HOWTO: A guide to shell scripting, using Bash | ||
---|---|---|
Prev | Chapter 3. Tutorial / Reference | Next |
Variables are at the heart of every programming and scripting language. They are used for arithmetic operations and manipulation of quantities, string parsing, and working in the abstract with symbols - tokens that represent something else. A variable is nothing more than a location or set of locations in computer memory that holds an item of data.
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 $, is when declared or assigned. 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. Using single quotes (' ') causes the variable name to be used literally, and no substitution will take place. This is full quoting.
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 3.3.1 below).
Example 3-5. Variable assignment and substitution
#!/bin/bash # Variables: assignment and substitution a=37.5 hello=$a # No space permitted on either side of = sign when initializing variables. echo hello # Not a reference. echo $hello echo ${hello} #Identical to above. echo "$hello" echo "${hello}" echo '$hello' # Variable referencing disabled by single quotes, # because $ interpreted literally. # Notice the effect of different types of quoting. # ------------------------------------------------ echo; echo numbers="one two three" other_numbers="1 2 3" # If whitespace within variables, then quotes necessary. echo "numbers = $numbers" echo "other_numbers = $other_numbers" echo echo "uninitialized variable = $uninitialized_variable" # Uninitialized variable has null value (no value at all). echo exit 0 |
Note: 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.
Same as $parameter, i.e., value of the variable parameter
May be used for concatenating variables with strings
your_id=${USER}-on-${HOSTNAME} echo "$your_id" |
If parameter not set, use default
echo ${username-`whoami`} # Echoes the result of `whoami`, but variable "username" is still unset. |
Note: This is almost equivalent to ${parameter:-default}. The extra : makes a difference only when parameter has been declared, but is null.
#!/bin/bash username0= echo "username0 = ${username0-`whoami`}" # username0 has been declared, but is set to null. # Will not echo. echo "username1 = ${username1-`whoami`}" # username1 has not been declared. # Will echo. username2= echo "username2 = ${username2:-`whoami`}" # username2 has been declared, but is set to null. # Will echo because of :- rather than just - in condition test. exit 0 |
If parameter not set, set it to default
Both forms nearly equivalent. The : makes a difference only when parameter has been declared and is null, as above.
echo ${username=`whoami`} # Variable "username" is now set to `whoami`. |
If parameter set, use 'otherwise", else use null string
Both forms nearly equivalent. The : makes a difference only when parameter has been declared and is null, as above.
If parameter set, use it, else print err_msg
Both forms nearly equivalent. The : makes a difference only when parameter has been declared and is null, as above.
Example 3-6. Using param substitution and :
#!/bin/bash # Let's check some of the system's environmental variables. # If, for example, $USER, the name of the person # at the console, is not set, the machine will not # recognize you. : ${HOSTNAME?} ${USER?} ${HOME} ${MAIL?} echo echo "Name of the machine is $HOSTNAME." echo "You are $USER." echo "Your home directory is $HOME." echo "Your mail INBOX is located in $MAIL." echo echo "If you are reading this message," echo "critical environmental variables have been set." echo echo # The ':' operator seems fairly error tolerant. # This script works even if the '$' omitted in front of # {HOSTNAME}, {USER?}, {HOME?}, and {MAIL?}. Why? # ------------------------------------------------------ # The ${variablename?} construction can also check # for variables set within the script. ThisVariable=Value-of-ThisVariable # Note, by the way, that string variables may be set # to characters disallowed in their names. : ${ThisVariable?} echo "Value of ThisVariable is $ThisVariable". echo echo # If ZZXy23AB has not been set... : ${ZZXy23AB?} # This will give you an error message and terminate. echo "You will not see this message." exit 0 |
Parameter substitution and/or expansion. The following expressions are the complement to the match in expr string operations (see Example 3-48). These particular ones are used mostly in parsing file path names.
Strip off shortest/longest part of pattern if it matches the front end of variable.
Strip off shortest/longest part of pattern if it matches the back end of variable.
Version 2 of bash adds additional options.
Example 3-7. Renaming file extensions:
#!/bin/bash # rfe # --- # Renaming file extensions. # # rfe old_extension new_extension # # Example: # To rename all *.gif files in working directory to *.jpg, # rfe gif jpg if [ $# -ne 2 ] then echo "Usage: `basename $0` old_file_suffix new_file_suffix" exit 1 fi for filename in *.$1 # Traverse list of files ending with 1st argument. do mv $filename ${filename%$1}$2 # Strip off part of filename matching 1st argument, # then append 2nd argument. done exit 0 |
variable var expanded, starting from offset pos.
expansion to a max of len characters of variable var, from offset pos.
first match of patt, within var replaced with replacement.
If replacement is omitted, then the first match of patt is replaced by nothing, that is, deleted.
all matches of patt, within var replaced with replacement.
Similar to above, if replacement is omitted, then all occurrences patt are replaced by nothing, that is, deleted.
Example 3-8. Using pattern matching to parse arbitrary strings
#!/bin/bash var1=abcd-1234-defg echo "var1 = $var1" t=${var1#*-*} echo "var1 (with everything, up to and including first - stripped out) = $t" t=${var1%*-*} echo "var1 (with everything from the last - on stripped out) = $t" echo path_name=/home/bozo/ideas/thoughts.for.today echo "path_name = $path_name" t=${path_name##/*/} # Same effect as t=`basename $path_name` echo "path_name, stripped of prefixes = $t" t=${path_name%/*.*} # Same effect as t=`dirname $path_name` echo "path_name, stripped of suffixes = $t" echo t=${path_name:11} echo "$path_name, with first 11 chars stripped off = $t" t=${path_name:11:5} echo "$path_name, with first 11 chars stripped off, length 5 = $t" echo t=${path_name/bozo/clown} echo "$path_name with \"bozo\" replaced by \"clown\" = $t" t=${path_name/today/} echo "$path_name with \"today\" deleted = $t" t=${path_name//o/O} echo "$path_name with all o's capitalized = $t" t=${path_name//o/} echo "$path_name with all o's deleted = $t" exit 0 |