Quoting & Escaping Parameters

9 min read·Jan 1, 2025

In Bash, quoting refers to the process of enclosing command parameters within single quotes ' and double quotes " to influence the way commands are interpreted and executed by the shell.

On the other hand, escaping refers to the process of selectively preventing shell expansions using the backslash character \.

Defining multi-words command parameters

By default, the shell uses whitespace characters, such as spaces, tabs, and newlines as delimiters to break down commands into distinct elements.

For example, the shell will break down this expression into 1 command and 2 parameters:

$ echo Hello World

Where:

  • echo is the command.
  • Hello and World are the parameters.

To force the shell to treat the string "Hello World" as a single parameter instead of two, you can wrap this expression into single quotes:

$ echo 'Hello World'
Hello World

Or double quotes:

$ echo "Hello World"
Hello World

Note: Upon execution, the quotes are not preserved by the shell as they are only used as a parameter delimiter and are not per se part of the expression.

Preserving whitespaces and tabs

By default, when enclosing multi-words parameters in quotes, the shell preserves all whitespaces and tabs between each quote.

Example

For example, when running this command, the whitespaces are automatically removed by the shell as the words "Hello" and "World" are treated as two parameters:

echo Hello          World
Hello World

Whereas when running this command, the whitespaces between the words "Hello" and "World" are preserved as the string "Hello     World" is treated as a single parameter:

$ echo "Hello     World"
Hello     World

Preventing shell expansions

In Bash, single quotes are literal, which means that all special characters and expansions, such as $? or !! will lose their meaning and be interpreted literally by the shell as simple characters.

Example

This command will expand the $? expression into the exit status of the previous command:

$ echo "$?"
0

Whereas this command will treat the $? expression as a literal string:

$ echo '$?'
$?

Escaping characters

In Bash, to selectively strip an expression from its special meaning and prevent its evaluation or expansion, you can prepend it with a backslash character \ called an escape character.

Example

This command will prevent the wildcard expansion:

$ echo *
data.csv     script.sh
$ echo \*
*

This command will prevent the brace expansion:

$ echo {1..3}
1 2 3
$ echo \{1..3}
{1..3}

This command will prevent the history expansion:

$ echo !2
echo Hello World
Hello World
$ echo \!2
!2

This command will prevent the tilde expansion:

$ echo ~
/home/johndoe
$ echo \~
~

This command will preserve the double quotes:

$ echo "Hello"
Hello
$ echo \"Hello\"
"Hello"

Adding newlines in command parameters

In Bash, single and double quotes can be used to insert newline characters within command arguments.

When opening a quote, and pressing the ENTER key, the shell will prompt you with a special command prompt > indicating you that the command continues on the next line:

$ command "argument
>

From here, whenever pressing the ENTER key, the shell will look for the matching quote and either execute the command if the quote is found:

$ command "argument
> argument"

Or display the multiline prompt once again on the next line, allowing you to continue typing your command:

$ command "argument
> argument
>

Upon execution, the shell will join all the lines into a single command and add a newline character between each line.

Example

This command will output the "Hello World" string to the terminal while preserving the newline character between the words "Hello" and "World":

$ echo "Hello
> World"
Hello
World

Adding newlines using a backslash escape character

Another way to add newlines into command parameters is to use the backslash escape character \n.

Example

This command will output the "Hello World" string to the terminal while adding a newline character between the words "Hello" and "World":

$ echo -e "Hello\nWorld"
Hello
World

Note: When using the echo command, the -e flag is used to enable the interpretation of backslash escape characters, which is disabled by default.

Writing commands on multiple lines

Beyond escaping special characters and expansions, the backslash character \ can be used to write long or complex commands on multiple lines.

When placing a backlash at the end of the command-line and pressing the ENTER key, the shell will prompt you with a special command prompt > indicating you that the command continues on the next line:

$ command \
>

You can then repeat the process by adding another backslash at the end of the new line and so on:

$ command \
> argument \
> argument

Upon execution, the shell will treat the newline as if it didn't exist, remove the backslash(s) from the command, and join all the lines into a single command.

Example

This command will output the string "This is a long string that spans multiple lines." in the terminal:

$ echo "This is a long \
> string that spans \
> multiple lines."
This is a long string that spans multiple lines.

Summary

Here's a summary of what you've learned in this lesson:

  • Single quotes '' and double quotes "" are used to form multi-words command parameters, where whitespaces and tabs are preserved.
  • Single quotes are literal and do not allow any shell expansions.
  • Double quotes allow various shell expansions.
  • The backslash character \ can be used to selectively prevent shell expansions when placed before special characters.
  • The backslash character can be used to write multi-lines commands when placed at the end of the command-line.
  • Unmatched quotes can be used to write command arguments with newlines.

Enjoying the courses?

I've made these courses completely free so anyone can learn from them. If they've helped you and you'd like to actively support the work behind BackendBrewery, you can leave a tip:

Support BackendBrewery
Quoting & Escaping Parameters in Bash | Backend Brewery