Rules for Naming variable name

From Linux Shell Scripting Tutorial - A Beginner's handbook
Jump to: navigation, search
← Default shell variables valueHomeEcho Command →

Variable name must begin with alphanumeric character or underscore character (_), followed by one or more alphanumeric or underscore characters. Valid shell variable examples:

HOME
SYSTEM_VERSION
vech
no

Do not put spaces on either side of the equal sign when assigning value to variable. For example, the following is valid variable declaration:

no=10

However, any of the following variable declaration will result into an error such as command not found:

no  =10
no=  10
no  =  10

Variables names are case-sensitive, just like filenames.

no=10
No=11
NO=20
nO=2

All are different variable names, to display value 20 you've to use $NO variable:

echo "$no" # print 10 but not 20
echo "$No" # print 11 but not 20
echo "$nO" # print 2 but not 20
echo "$NO" # print 20

You can define a NULL variable as follows (NULL variable is variable which has no value at the time of definition):

vech=
vech=""

Try to print its value by issuing the following command:

echo $vech

Do not use ?,* and other special characters, to name your variable.

?no=10 #invalid
out*put=/tmp/filename.txt #invalid
_GREP=/usr/bin/grep #valid
echo "$_GREP"
← Default shell variables valueHomeEcho Command →