I am trying to figure out what is the difference between " " and ' '.
When I use them with echo
they both give the same result,
sps@sps-Inspiron-N5110:~$ echo Hello world
Hello world
sps@sps-Inspiron-N5110:~$ echo "Hello world"
Hello world
sps@sps-Inspiron-N5110:~$ echo 'Hello world'
Hello world
sps@sps-Inspiron-N5110:~$
Is there any specific situation where we need to use one or the other ?
Thanks.
In bash (Ubuntu's default interactive shell), everything following an open single quote (
'
) is quoted literally, up until the next'
which is taken as the closing quote. In contrast, several kinds of shell expansions are performed inside double quotes ("
).Parameter expansion (with
$...
or${...}
):This is a common use case for
"
quotes in shell scripting. In particular, you may have seen loops that iterate through filenames, expanding a variable inside double quotes so it does get expanded but doesn't have its value split into separate words. For example, a command like this might help me keep track of things while preparing lots of media files for moving:Command substitution (with
$(...)
or`...`
) and arithmetic expansion (with$((...))
):Escaping single characters with
\
is supported and can be handy in"
quotes, but is unsupported (and would generally be pointless) in the simpler'
quotes:History expansion with
!
(if enabled, which it usually is when you're using the shell interactively):In addition, one of the most common reasons to use double quotes are to easily and intuitively quote text containing single quotes (since a
'
has no special meaning at all, inside"
quotes):Finally, though you cannot escape a
'
inside'
-quoted text, you can achieve the same result by ending quoting with'
, using an escaped'
(\'
), and resuming quoting with'
:stackoverflow link posted above by bodhi.zazen answers this question.
Below is one situtation where it makes difference.
Posting this answer, just to let others know that I've got the answer already. (I had posted this question .)
Thanks.