In Bash scripting, a variable is like a container that holds a value, or data. The name you give to the variable acts as a placeholder for that value. When you want to retrieve the value stored in a variable, it’s called “variable substitution.”
Distinguishing Variable Names and Values
It’s crucial to differentiate between a variable’s name and its value. For instance, if variable1
is the name, then $variable1
represents its value—the actual data it contains.
Here’s a simple example:
Bash
variable1=23
echo variable1 # Output: variable1
echo $variable1 # Output: 23
When Variables Appear “Naked”
A variable appears without the $
prefix only in specific situations:
- Declaration or Assignment: When you create or assign a value to a variable (e.g.,
var1=27
). - Unsetting or Exporting: When you use the
unset
orexport
commands. - Arithmetic Expressions: Inside double parentheses
(( ... ))
. - Special Cases: When representing a signal or in loop headers (e.g.,
for var2 in 1 2 3
).
Quoting and Variable Substitution
- Double Quotes (“…”): Allow variable substitution. This is known as “partial” or “weak” quoting.
- Single Quotes (‘…’): Treat the variable name literally, preventing substitution. This is “full” or “strong” quoting.
Note that $variable
is a shorthand for ${variable}
. The longer form can be helpful in situations where the shorter form causes errors.
Example: Variable Assignment and Substitution
Bash
#!/bin/bash
a=375
hello=$a
echo hello # Output: hello
echo $hello # Output: 375
echo ${hello} # Output: 375
echo "$hello" # Output: 375
echo "${hello}" # Output: 375
hello="A B C D"
echo $hello # Output: A B C D
echo "$hello" # Output: A B C D
hello=
echo "\$hello (null value) = $hello" # Output: $hello (null value) =
var1=21 var2=22 var3=$V3
echo "var1=$var1 var2=$var2 var3=$var3"
numbers="one two three"
other_numbers="1 2 3"
echo "numbers = $numbers"
echo "other_numbers = $other_numbers"
mixed_bag=2\ ---\ Whatever
echo "$mixed_bag" # Output: 2 --- Whatever
echo "uninitialized_variable = $uninitialized_variable"
uninitialized_variable=
echo "uninitialized_variable = $uninitialized_variable"
uninitialized_variable=23
unset uninitialized_variable
echo "uninitialized_variable = $uninitialized_variable"
exit 0
Key Points:
- An uninitialized variable has a null value.
- Setting a variable to a null value is different from unsetting it.
- Quoting preserves whitespace.
- Bash variables are untyped, meaning they can hold strings or numbers.
Variable Types
Bash uses different types of variables:
- Local Variables: Visible only within a specific code block or function.
- Environmental Variables: Affect the behavior of the shell and user interface.
- Positional Parameters: Arguments passed to a script from the command line.
Positional Parameters
$0
: The name of the script.$1
,$2
,$3
, etc.: The arguments passed to the script.$*
and$@
: All positional parameters.- Parameters beyond
$9
must be enclosed in curly braces, e.g.,${10}
.
Example: Positional Parameters
Bash
#!/bin/bash
MINPARAMS=10
echo "The name of this script is \"$0\"."
echo "The name of this script is \"`basename $0`\"."
if [ -n "$1" ]; then echo "Parameter #1 is $1"; fi
if [ -n "$2" ]; then echo "Parameter #2 is $2"; fi
if [ -n "${10}" ]; then echo "Parameter #10 is ${10}"; fi
echo "-----------------------------------"
echo "All the command-line parameters are: $*"
if [ $# -lt "$MINPARAMS" ]; then
echo "This script needs at least $MINPARAMS command-line arguments!"
fi
exit 0
The shift
Command
The shift
command reassigns positional parameters, effectively shifting them to the left. This is useful for processing a large number of arguments.
Example: Using shift
Bash
#!/bin/bash
until [ -z "$1" ]; do
echo -n "$1 "
shift
done
echo
exit
Important Notes:
- Pay attention to quoting to control variable substitution.
- Be mindful of the differences between local, environmental, and positional variables.
- Use
shift
to efficiently process command-line arguments. - Always test your scripts thoroughly to avoid unexpected behavior.
No responses yet