Wednesday, April 6, 2011

How to quote the value of the variables appropriately?

First of all, there are two type of quoting. Each one has its own significance. We will see that through examples.

Types of quoting:
a.) Single quoting
b.) Double quoting

When to use single quote?
If you want the value to be treated as exactly entered. Then, should go for quoting through single quotes. But there are some special characters which will not follow the rule thought it is entered inside single quotes.

Eg:
volcano@volcano-laptop:~/shellscript$ os='sun solaris'
volcano@volcano-laptop:~/shellscript$ echo $os
sun solaris
volcano@volcano-laptop:~/shellscript$ echo 'another operating system is Linux'
another operating system is Linux

When to use double quote?
If you want special characters expanded properly while it is part of the value of variable.Then double quote should be used.

volcano@volcano-laptop:~/shellscript$ logged_user=$USER
volcano@volcano-laptop:~/shellscript$ echo "Logged in user is $logged_user" Logged in user is volcano

1 comment: