Positional Parameters
Positional parameters allow scripts to accept command-line arguments:
$0- Name of the currently executing script$n- nth argument passed to the script (use braces for n > 10, e.g.,${10})$#- Number of arguments passed to the script$*- All arguments as a single string$@- All arguments as separate strings
When unquoted, $* and $@ behave identically. When quoted, "$*" combines all arguments into a single string, while "$@" preserves each argument as a separate string.
Example: Parameter Validation
#!/bin/bash
if [ $# -ne 2 ]; then
echo "Usage: $0 arg1 arg2"
exit 1
fi
echo "Arguments received: $1 $2"
When executed without arguments:
$ ./script.sh
Usage: ./script.sh arg1 arg2
When executed with two arguments:
$ ./script.sh hello world
Arguments received: hello world
Status Variables
These variables provide information about the shell and executed commands:
| Variable | Description |
|---|---|
$? |
Exit status of the last command (0 = success, non-zero = failure) |
$$ |
Process ID (PID) of the current script |
$! |
PID of the last background process |
$_ |
Last argument of the previous command |
Exit Status Handling
The $? variable is crucial for error handling in scripts. Different commands may return different exit codes, but 0 typically indicates success while any other value indicates failure.
Common uses of $? include:
- Verifying if commands executed successfully
- Checking backup operations completed without errors
- Controlling script flow based on command results
Example: Exit Status in Function
stop_service() {
echo -n "Stopping $service: "
killproc $service
local result=$?
echo
if [ $result -eq 0 ]; then
rm -f "/var/lock/subsys/$service"
rm -f "/var/run/$service*"
fi
return $result
}
Parameter Expansion Operators
These operators allow for more sophisticated variable handling:
| Operator | Description |
|---|---|
${var:-default} |
Use default if variable is unset or empty |
${var:=default} |
Set variable to default if unset or empty |
${var:+alternate} |
Use alternate if variable is set and not empty |
${var:?error_message} |
Display error and exit if variable is unset or empty |
When the colon is omitted (e.g., ${var-default}), the check applies only to unset variables. When included (e.g., ${var:-default}), it also checks for empty variables.
Example: Parameter Expansion
#!/bin/bash
# Test default value substitution
echo "Unset test: ${unset_var:-default_value}"
echo "After substitution: $unset_var"
# Test variable assignment
echo "Assignment test: ${unset_var:=assigned_value}"
echo "After assignment: $unset_var"
# Test alternate value usage
echo "Alternate test: ${unset_var:+alternate_value}"
echo "Variable remains: $unset_var"
# Test error message
unset unset_var
echo "Error test: ${unset_var:?Variable is not set}"
Practical Application
These operators are particularly useful for handling optional configuration parameters:
# Search for old files in specified directory or /tmp
find "${search_path:-/tmp}" -name "*.tar.gz" -type f -mtime +7 | xargs rm -f
This ensures that even if search_path is unset, the command will default to /tmp, preventing errors and maintaining functionality.