Skip to content

Bash Variable Expansion Not Working Fix

DodaTech Updated 2026-06-24 2 min read

In this tutorial, you'll learn about Bash Variable Expansion Not Working Fix. We cover key concepts, practical examples, and best practices to help you understand and apply this topic effectively.

Bash variable expansion fails when single quotes prevent substitution, curly braces are missing, or when using indirect expansion incorrectly.

The Wrong Way

name="Alice"
echo 'Hello, $name'

Output:

Hello, $name

Single quotes prevent variable expansion. The literal string $name is printed.

The Right Way

name="Alice"
echo "Hello, $name"

Output:

Hello, Alice

Double quotes allow variable expansion.

Step-by-Step Fix

1. Use double quotes for variable expansion

echo "Hello, $name"
echo "Value: ${variable}"

2. Use curly braces for complex expansions

echo "${prefix}_suffix"
echo "${array[index]}"
echo "${default:-fallback}"

3. Use ${!var} for indirect expansion

varname="name"
name="Alice"
echo "${!varname}"  # prints Alice

4. Escape $ inside double quotes when needed

price=100
echo "The price is \$100"

5. Use printf for formatted output

printf "Hello, %s\n" "$name"

Prevention Tips

  • Use double quotes for strings that contain variables.
  • Use single quotes for literal strings with no variables.
  • Use curly braces ${var} when variables are adjacent to other characters.
  • Use ${!var} for indirect variable references.
  • Use set -u to catch typos in variable names.

Common Mistakes with variable expansion

  1. Overlapping type class instances that cause GHC to reject the program with ambiguous dispatch errors
  2. Non-exhaustive pattern matches that compile with warnings then crash at runtime
  3. Misunderstanding that String is [Char] with poor performance for large text operations

These mistakes appear frequently in real-world BASH code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.

Practice Exercise

Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.

This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.

FAQ

### What is the difference between single and double quotes in Bash?

Single quotes preserve every character literally -- no variable expansion, no escape sequences. Double quotes allow variable expansion ($var), command substitution ($(cmd)), and some escape sequences.

When do I need curly braces around variable names?

Curly braces are needed when the variable name is followed by a character that could be interpreted as part of the variable name. Example: echo "${name}_file" vs echo "$name_file" (different variable name).

How do I use a variable as a variable name (indirect expansion)?

Use ${!varname} syntax, where varname holds the name of the target variable. Example: varname="PATH"; echo "${!varname}" prints the value of $PATH.

Built by the developers of Doda Browser, DodaZIP, and Durga Antivirus Pro.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro