Linux bash: A note about quotes

A note about quotes

Single quote: ‘ ‘ supresses all special characters.


root@bt:/# echo ‘$?’

$? is used to know the exit status, because $? is enclosed within a pair of single quotes all special meaning has been suppressed and printed out as it is.

Double quote: ” ” supresses most of the special characters except for $, \ and `.

If a single $ is enclosed within ” ” double quotes $ will still be printed as $ itself has no special meaning, however if $ is sub-ceded with other characters, numbers or symbols bash treated it with special meaning.

Example of single $ enclosed within double quotes:

root@bt:/# echo “$”

Example of $ sub-ceded with another character and enclosed within double quotes:

root@bt:/# echo “$?”

0 is the exit status, 0 means normal.

Another example of $ sub-ceded by another character:

root@bt:/# echo “$(ifconfig eth0)”
eth0 Link encap:Ethernet HWaddr e8:11:32:41:88:9d 
inet addr: Bcast: Mask:
inet6 addr: fe80::ea11:32ff:fe41:889d/64 Scope:Link
RX packets:57864 errors:0 dropped:0 overruns:0 frame:0
TX packets:4520 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000 
RX bytes:11899324 (11.8 MB) TX bytes:615368 (615.3 KB)
Interrupt:41 Base address:0xe000

As you can see bash treated $ specially. However compare with another example using a pair of single quotes:

root@bt:/# echo ‘$(ifconfig eth0)’
$(ifconfig eth0)

I hope this illustration is clear enough, this is very important because while writing a bash script desired results might not be achieved due to the misused of quotes.

This entry was posted in Bash, Linux, Scripting, System OS and tagged , , , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s