In Bash, what are the differences between single quotes (''
) and double quotes (""
)?
相关问题
- How to get the return code of a shell script in lu
- What does an “empty line with semicolon” mean in C
- JQ: Select when attribute value exists in a bash a
- Invoking Mirth Connect CLI with Powershell script
- Emacs shell: save commit message
相关文章
- 使用2台跳板机的情况下如何使用scp传文件
- In IntelliJ IDEA, how can I create a key binding t
- Check if directory exists on remote machine with s
- shell中反引号 `` 赋值变量问题
- How get the time in milliseconds in FreeBSD?
- Reverse four length of letters with sed in unix
- What is the meaning of this syntax?
- Launch interactive SSH bash session from PHP
Others explained very well and just want to give with simple examples.
Single quotes can be used around text to prevent the shell from interpreting any special characters. Dollar signs, spaces, ampersands, asterisks and other special characters are all ignored when enclosed within single quotes.
It will give this:
The only thing that cannot be put within single quotes is a single quote.
Double quotes act similarly to single quotes, except double quotes still allow the shell to interpret dollar signs, back quotes and backslashes. It is already known that backslashes prevent a single special character from being interpreted. This can be useful within double quotes if a dollar sign needs to be used as text instead of for a variable. It also allows double quotes to be escaped so they are not interpreted as the end of a quoted string.
It will give this:
It may also be noticed that the apostrophe, which would otherwise be interpreted as the beginning of a quoted string, is ignored within double quotes. Variables, however, are interpreted and substituted with their values within double quotes.
It will give this:
Back quotes are wholly unlike single or double quotes. Instead of being used to prevent the interpretation of special characters, back quotes actually force the execution of the commands they enclose. After the enclosed commands are executed, their output is substituted in place of the back quotes in the original line. This will be clearer with an example.
It will give this:
There is a clear distinction between the usage of
' '
and" "
.When
' '
is used around anything, there is no "transformation or translation" done. It is printed as it is.With
" "
, whatever it surrounds, is "translated or transformed" into its value.By translation/ transformation I mean the following: Anything within the single quotes will not be "translated" to their values. They will be taken as they are inside quotes. Example:
a=23
, thenecho '$a'
will produce$a
on standard output. Whereasecho "$a"
will produce23
on standard output.The accepted answer is great. I am making a table that helps in quick comprehension of the topic. The explanation involves a simple variable
a
as well as an indexed arrayarr
.If we set
and then
echo
the expression in the second column, we would get the result / behavior shown in the third column. The fourth column explains the behavior.See also:
$''
- GNU Bash Manual$""
- GNU Bash ManualSingle quotes won't interpolate anything, but double quotes will. For example: variables, backticks, certain
\
escapes, etc.Example:
The Bash manual has this to say:
If you're referring to what happens when you echo something, the single quotes will literally echo what you have between them, while the double quotes will evaluate variables between them and output the value of the variable.
For example, this
will give this: