How to assign default values to shell variables with a single command in bash ?

In Bash, you can assign default values to variables in a single command using parameter expansion syntax. This allows you to set a fallback value if the variable is unset or null. Below are the key methods with examples:

1. Assign Default if Variable is Unset or Null

Use the :- operator to set a default value temporarily (only for the current expansion).
To permanently assign the default to the variable, combine it with an assignment:

# Syntax: Assign default if variable is unset or empty
variable="${variable:-default_value}"

# Example:
name="${name:-John}"  # If $name is unset/null, set it to "John"

2. Assign Default if Variable is Unset (Ignore Null Values)

Use the - operator to set a default only if the variable is unset (ignores null/empty values):

# Syntax: Assign default only if variable is unset (not for empty values)
variable="${variable-default_value}"

# Example:
count="${count-0}"  # Sets $count to 0 only if it was never defined

3. Assign and Export in One Line

Use := to set a default and update the variable permanently in a single step.
This works even for variables that are null (empty):

# Syntax: Permanently assign default if variable is unset or null
: "${variable:=default_value}"  # Colon (:) is a no-op command that evaluates the assignment

# Example:
: "${PATH:=/usr/local/bin}"  # Set $PATH to /usr/local/bin if it's unset/null

4. Assign Defaults for Positional Parameters

Set defaults for script arguments ($1, $2, etc.):

# Syntax: Set default for the first argument
param1="${1:-default}"

# Example (script.sh):
#!/bin/bash
name="${1:-Guest}"  # Use "Guest" if no argument is provided
echo "Hello, $name!"

Run it:

./script.sh       # Output: Hello, Guest!
./script.sh Alice # Output: Hello, Alice!

5. Nested Defaults

Chain defaults for multiple fallback values:

# Use the first non-empty value in a chain
value="${VAR1:-${VAR2:-fallback}}"

# Example:
color="${USER_COLOR:-${DEFAULT_COLOR:-blue}}"

Key Operators

OperatorDescription
${var:-x}Use x if var is unset or null (does not modify var).
${var-x}Use x only if var is unset (ignores null values).
${var:=x}Set var to x if unset or null (permanently updates var).
${var=x}Set var to x only if unset (ignores null values).

Examples

Set a Default Port

: "${PORT:=8080}"  # Set $PORT to 8080 if unset/null
echo "Using port: $PORT"

Handle Optional Configurations

# Use $USER_CONFIG if set, otherwise "config/default.yml"
config_file="${USER_CONFIG:-config/default.yml}"

Fallback for Empty Strings

# If $message is empty/null, use "No message provided"
echo "${message:-No message provided}"

Why This Matters

  • Avoid Errors: Prevent “unbound variable” errors in scripts.
  • Simplify Code: Replace verbose if checks with one-liners.
  • Flexibility: Set defaults dynamically based on conditions.

By mastering these techniques, you can write cleaner, more robust Bash scripts!

Leave a Reply

Your email address will not be published. Required fields are marked *