Learn how quoting works in Bash and how it affects command arguments.
In Bash, quoting is used to control how the shell interprets characters and words.
Quotes can remove special meaning from certain characters, preserve whitespace, and control how arguments are passed to commands.
In this concept, we'll learn how different types of quotes work and how Bash processes quoted strings.
In Bash, quoting is used to control how the shell interprets characters and words.
Quotes can remove special meaning from certain characters, preserve whitespace, and control how arguments are passed to commands.
In this concept, we'll learn how different types of quotes work and how Bash processes quoted strings.
By default, spaces act as delimiters that separate command arguments:
# Switch to Bash if needed
$ bash
$ echo hello world
hello world
This passes two separate arguments to echo: hello and world.
Single quotes (') preserve the literal value of every character within them.
$ echo hello world
hello world
$ echo 'hello world'
hello world
Here, the spaces within the quote are preserved and don't act as a delimiter, so echo receives only one argument: hello world
Single quotes can be used to prevent the shell from interpreting special characters like $:
$ echo $100 costs more than $50
00 costs more than 0
$ echo '$100 costs more than $50'
$100 costs more than $50
Double quotes (") preserve most characters but allow certain special characters to be interpreted (like $ and \).
$ echo "hello world"
hello world
They can be used to preserve whitespace or include variables in strings:
$ name="Alice"
$ echo "Hello $name"
Hello Alice
Since spaces are the only delimiter that separates arguments, any strings written without spaces between them are treated as a single argument.
This means adjacent strings are concatenated together:
$ echo "hello""world"
helloworld
This concatenates "hello" and "world" into a single argument: helloworld.
Quoted and unquoted text can also be mixed together, and they'll be concatenated:
$ echo "hello"world
helloworld
Once a space appears between strings, they become separate arguments:
$ echo "hello" world
hello world
The backslash (\) can escape a single character, removing its special meaning.
$ echo three\ \ \ spaces
three spaces
In this example, the backslashes escapes each space, making them part of the string instead of a separator.
For unquoted strings, a backslash will simply escape the next character:
$ echo \$100
$100
$ echo abc\de
abcde
Inside double quotes, the backslash only escapes certain special characters ($, `, ", \, and newline):
$ echo "hello \"world\""
hello "world"
For other characters, the backslash is treated literally:
$ echo "abc\de"
abc\de
You've now learnt how quoting works in the Bash shell.
A quick recap of what we've covered:
For more details on quoting, check out the Bash manual.