The OpenNET Project / Index page

[ новости /+++ | форум | wiki | теги | ]

Поиск:  Каталог документации

3.3. Introduction to Variables and Parameters

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


# Variables: assignment and substitution

# 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 "uninitialized variable = $uninitialized_variable"
# Uninitialized variable has null value (no value at all).


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.

3.3.1. Parameter Substitution


Same as $parameter, i.e., value of the variable parameter

May be used for concatenating variables with strings

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.


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.

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

${parameter=default}, ${parameter:=default}

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`.

${parameter+otherwise}, ${parameter:+otherwise}

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.

${parameter?err_msg}, ${parameter:?err_msg}

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 :


# 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 "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 "If you are reading this message,"
  echo "critical environmental variables have been set."

# 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.

# Note, by the way, that string variables may be set
# to characters disallowed in their names.
: ${ThisVariable?}
echo "Value of ThisVariable is $ThisVariable".

# 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.

${var#pattern}, ${var##pattern}

Strip off shortest/longest part of pattern if it matches the front end of variable.

${var%pattern}, ${var%%pattern}

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:


#                 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 ]
  echo "Usage: `basename $0` old_file_suffix new_file_suffix"
  exit 1

for filename in *.$1
# Traverse list of files ending with 1st argument.
  mv $filename ${filename%$1}$2
  # Strip off part of filename matching 1st argument,
  # then append 2nd argument.

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


echo "var1 = $var1"

echo "var1 (with everything, up to and including first - stripped out) = $t"
echo "var1 (with everything from the last - on stripped out) = $t"


echo "path_name = $path_name"
# Same effect as   t=`basename $path_name`
echo "path_name, stripped of prefixes = $t"
# Same effect as   t=`dirname $path_name`
echo "path_name, stripped of suffixes = $t"


echo "$path_name, with first 11 chars stripped off = $t"
echo "$path_name, with first 11 chars stripped off, length 5 = $t"


echo "$path_name with \"bozo\" replaced  by \"clown\" = $t"
echo "$path_name with \"today\" deleted = $t"
echo "$path_name with all o's capitalized = $t"
echo "$path_name with all o's deleted = $t"

exit 0

Inferno Solutions
Hosting by

Закладки на сайте
Проследить за страницей
Created 1996-2022 by Maxim Chirkov
Добавить, Поддержать, Вебмастеру