Linux bash: A note about quotes

A note about quotes

Single quote: ‘ ‘ supresses all special characters.

Example:

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

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:90.0.0.118 Bcast:90.0.0.255 Mask:255.255.255.0
inet6 addr: fe80::ea11:32ff:fe41:889d/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
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.

Advertisement

Leave a Reply

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

WordPress.com Logo

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

Facebook photo

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

Connecting to %s