---
title: "Single vs double quotes in Bash, and why you should quote your variables"
description: "Single quotes keep text literal, double quotes still expand variables. Learn the difference and why unquoted variables break on spaces and wildcards."
url: https://www.danny.engineering/answer/bash/single-vs-double-quotes-in-bash-and-why-you-should-quote-your-variables
author: Danny
published: 2026-11-27T00:00:00.000Z
updated: 2026-11-27T17:25:00.000Z
tags: ["Bash"]
---

# Single vs double quotes in Bash, and why you should quote your variables

## The short answer


Single quotes keep everything inside them literal. Double quotes still expand variables, command substitutions and arithmetic, but stop the result being split on spaces or treated as a wildcard. Wrap variables in double quotes almost every time you use them.


```bash
name="Ada Lovelace"

echo '$name'      # prints: $name
echo "$name"      # prints: Ada Lovelace

touch "$name.txt" # one file called "Ada Lovelace.txt"
touch $name.txt   # two files: "Ada" and "Lovelace.txt"
```


## Why it happens


Bash processes a command line in stages. After it expands a variable, it does two more things to the result if the expansion was **not** in double quotes:

1. **Word splitting.** The value is split into separate words on the characters in `IFS`, which by default are space, tab and newline.
2. **Pathname expansion (globbing).** Each word that contains `*`, `?` or `[` is treated as a pattern and replaced with matching file names.

So an unquoted variable is not one value. It is "whatever this turns into after splitting and globbing". That is rarely what you meant:


```bash
file="My Report.pdf"
rm $file          # tries to remove "My" and "Report.pdf"

pattern="*"
echo $pattern     # lists every file in the current directory
echo "$pattern"   # prints: *
```


The three kinds of quoting behave like this:


| Quoting       | Variables expand? | Command substitution? | Split and glob? |
| ------------- | ----------------- | --------------------- | --------------- |
| None          | Yes               | Yes                   | Yes             |
| Double quotes | Yes               | Yes                   | No              |
| Single quotes | No                | No                    | No              |


Inside double quotes, only `$`, backticks, `\` and `"` keep a special meaning (and `!` in an interactive shell with history expansion on). Inside single quotes, nothing does, not even a backslash.


## How to fix it


### Double quote every expansion


Treat this as the default for variables, command substitutions and arithmetic results:


```bash
cp "$src" "$dest"
files_count="$(ls | wc -l)"
cd "$(dirname "$0")"
```


Nested quotes inside `$(...)` are fine. Each command substitution starts a fresh quoting context.


### Use "$@" to pass arguments through


`"$@"` expands to every positional argument as a separate word, keeping spaces inside each one. `$*` and unquoted `$@` both lose that.


```bash
wrapper() {
  echo "Running with $# arguments"
  real_command "$@"
}

wrapper "one arg" "two"   # real_command gets exactly 2 arguments
```


The same applies to arrays: `"${files[@]}"` gives one word per element.


### Use single quotes for literal text


Single quotes are ideal when the text contains characters you do not want Bash to touch, such as regular expressions, JSON, or another program's `$` syntax:


```bash
grep -E '^[0-9]+\.[0-9]+$' versions.txt
jq '.dependencies | keys' package.json
awk '{ print $2 }' data.txt
```


You cannot put a single quote inside single quotes, even escaped. Close the quote, add an escaped quote, and reopen it, or use double quotes:


```bash
echo 'It'\''s working'
echo "It's working"
```


Bash also supports ANSI-C quoting with `$'...'`, which understands escapes such as `\n` and `\t`:


```bash
printf '%s\n' $'line one\nline two'
```


### Mix them when you need both


Adjacent quoted strings join into one word, so you can switch quoting styles mid-argument:


```bash
echo 'Price: $5 for '"$name"
```


### Let ShellCheck catch the rest


[ShellCheck](https://www.shellcheck.net/) flags unquoted variables (warning SC2086) and many related mistakes. Running it in your editor or CI catches most quoting bugs before they reach a real file with a space in its name.


## Common pitfalls

- Assuming it works because your test file names had no spaces. Quote anyway; real paths will eventually contain spaces, brackets or asterisks.
- Quoting a tilde: `"~/projects"` is not expanded. Use `"$HOME/projects"` or leave the tilde outside the quotes, as in `~/"my projects"`.
- Quoting a glob you want to expand: `ls "*.txt"` looks for a file literally named `*.txt`. Leave the pattern unquoted and quote only the variable parts, as in `ls "$dir"/*.txt`.
- Writing `[ $var = "yes" ]` with an empty variable. The test becomes `[ = "yes" ]` and errors. Quote it, or use `[[ ... ]]`, which does not word-split its operands.
- Expecting single quotes to expand `$HOME` or `$(date)` inside an `echo` or an alias. They will not.

## Related reading

- [Bash Reference Manual: Quoting](https://www.gnu.org/software/bash/manual/html_node/Quoting.html)
- [Bash Reference Manual: Word Splitting](https://www.gnu.org/software/bash/manual/html_node/Word-Splitting.html)
