Friday, April 10, 2009

Rules for naming variable name in Unix

Before coding shell script it is very important that you would have an idea about the rules of the naming variable name in unix. You should need special care about this post because in shell script naming of variables are different than other usual programming language. Step by steps rules are discussed.

1)Variable names must begin with underscore or any alphanumeric characters. Never use ?,*,#,$ etc to name your variables.

Following is valid variable name.
username
password


2)While assigning value to a variable never use space on the either side of the equal sign(=).
The following one is valid,
a=20
But the followings are invalid as there is space after or before equal sign.
a= 20
a =20
a = 20

Assigning value in this way (space before or after equal) will cause error "command not found".

3)While comparing value with a variable note that between comparison operator there must space and also between bracket and variable there is space.
The following one is valid,
if [ $v = 10 ]
But the followings are invalid as there is no space between bracket and variable or between comparison operator and variable.
if [ $v=10 ]
if [$v = 10]
if [$v =10 ]

are invalid.

4)As of linux file system variables are case sensitive. So num and Num are different.

5)In order to assign a null value to a variable you can do following. You can assign null value to a variable at the time of variable declaration.
To assign null value to a variable test issue,
test=
or,
test=""
Related Documentation

No comments:

Post a Comment