0
0
Bash Scriptingscripting~5 mins

Double quotes (variable expansion) in Bash Scripting

Choose your learning style9 modes available
Introduction
Double quotes let you use variables inside text so the shell replaces them with their values.
You want to print a message that includes a variable value.
You need to combine text and variables in a command.
You want to keep spaces inside a variable when using it.
You want to avoid word splitting but still expand variables.
You want to include special characters inside a string with variables.
Syntax
Bash Scripting
echo "Hello, $USER!"
Use double quotes to allow variables like $USER to be replaced by their values.
Double quotes keep spaces and special characters inside the string intact.
Examples
Prints 'Hello, Alice!' by replacing $name with its value.
Bash Scripting
name="Alice"
echo "Hello, $name!"
Shows how a number variable is expanded inside double quotes.
Bash Scripting
count=5
echo "You have $count new messages."
Keeps the space inside the variable when printing.
Bash Scripting
path="/home/user folder"
echo "Path is: $path"
Sample Program
This script sets two variables and prints a welcome message using double quotes to expand them.
Bash Scripting
#!/bin/bash

user="Bob"
message="Welcome to the system"
echo "$message, $user!"
OutputSuccess
Important Notes
If you use single quotes (' '), variables will NOT expand.
Double quotes protect spaces and special characters but still allow variable expansion.
To include a literal dollar sign, escape it with a backslash like \$.
Summary
Double quotes let variables inside strings expand to their values.
They keep spaces and special characters safe inside the string.
Use double quotes when you want to mix text and variables in bash.