May 15, 2006

What is the difference between a single-quote, a quote, and a back-tick in the shell?

Filed under: Linux, FreeBSD, *NIX, Bash, Solaris, Interview Question — Administrator @ 5:12 pm

Single quote: variables ($), backticks (“) & backslash (\) are not treated specially in single quote.
Example:

zia@lappy:~$ echo ‘$(ls -al t*)’
$(ls -al t*)
zia@lappy:~$ echo ‘`ls -lah t*`’
`ls -lah t*`
zia@lappy:~$ echo ‘`ls -lah t*` \”‘
`ls -lah t*` \”

Synopsis: with single-quote, the special characters (i.e. $, “, \ etc.) are not treated specially, they are treated literally.

Double quote: variables ($), backticks (“) & backslash (\) are treated specially or interpreted other than its literal meaning.
For example:

zia@lappy:~$ echo “$(ls -al t*)”
-rwxr-xr-x 1 zia zia 449 2006-05-03 17:07 t
-rw-r–r– 1 root root 3365 2006-03-24 12:31 target.xml
zia@lappy:~$ echo “`ls -lah t*`”
-rwxr-xr-x 1 zia zia 449 2006-05-03 17:07 t
-rw-r–r– 1 root root 3.3K 2006-03-24 12:31 target.xml
zia@lappy:~$ echo “`ls -lah t*` \”"
-rwxr-xr-x 1 zia zia 449 2006-05-03 17:07 t
-rw-r–r– 1 root root 3.3K 2006-03-24 12:31 target.xml “

Synopsis: with double quote (”"), the special characters do exactly what they are supposed to do.

Leave a Reply

Proudly powered by wordpress - Theme by neuro