Tuesday, January 1, 2013

Global & Local Variable in Shel Script

The below code snippet shows how can we declare global and local variables in shell scripts.

#!/bin/bash
x=10
increment()
{
 echo "global variable : before increment value x=$x"
 (( x++ ))
}
increment
echo -e "global variable : after increment value x=$x\n"

localVar()
{
 local var_local=50;
 echo "inside localVar function value of var_local = $var_local"
}
localVar
echo "inside localVar function value of var_local = $var_local"


OUTPUT :

-bash-3.2$ sh local_global_var_ex
global variable : before increment value x=10
global variable : after increment value x=11
inside localVar function value of var_local = 50
inside localVar function value of var_local =
global variable is declared outside the function
local variable is delcared in side the function with key word local.
we cant use local variable outside the function but global variable can use inside/outside the function

No comments:

Post a Comment

Item - Category Query

      SELECT      msi.segment1 AS Item_Code,       msi.DESCRIPTION AS Item_Desc,       mcs.CATEGORY_SET_NAME,       mck.CONCATENATED_SEGMEN...