This widget could not be displayed.
Operating System - HP-UX
1859573 Members
6286 Online
110403 Solutions
This widget could not be displayed.
This widget could not be displayed.
This widget could not be displayed.
This widget could not be displayed.
This widget could not be displayed.
This widget could not be displayed.
This widget could not be displayed.
This widget could not be displayed.
This widget could not be displayed.

Re: defining a global variable

 
SOLVED
Go to solution
Pando
Regular Advisor

defining a global variable

i would like to ask hot to define a global and local variable in a script?
11 REPLIES 11
Noel Miranda
Frequent Advisor

Re: defining a global variable

What exactly do you mean by global?
Indira Aramandla
Honored Contributor

Re: defining a global variable

Hi Fernando,

A global variable is global to the script and can be accessed anywhere in the script. A local variable is local to a function or a loop which can be referred in the function or the loop.
eg:

count=0; is defining the variable counter as number and initialising to zero.
counterflag="y" is defining the variable counterflag as character and initialising to "y"

IA



Never give up, Keep Trying
Peter Godron
Honored Contributor

Re: defining a global variable

Fernando,
can you please expand on what you want to know.
Do you want to know how variables are used with a script (i.e. passing data between procedures with the same script)
or
how to change variables in your environment with a script (i.e. ORACLE_HOME etc).
Variables defined in script are local to that script, unless exported.
I believe all variables in a script are global to that script:
#!/usr/bin/sh
testing()
{
a=2
echo "Testing [$a]"
}
a=0
echo "main [$a]"
testing
echo "main [$a]"

will result in:
main [0]
Testing [2]
main [2]

Regards

Regards

Pando
Regular Advisor

Re: defining a global variable

Basically, i want to know how variables are used within a script (i.e. passing data between procedures with the same script)? Are they accessible anywhere within the script?
Leif Halvarsson_2
Honored Contributor
Solution

Re: defining a global variable

Hi,

Just export the variable.
Peter Godron
Honored Contributor

Re: defining a global variable

Fernando,
see my earlier example.
The variable (a) is visible throughout the script.
Regards
Artyom Voronchihin
Respected Contributor

Re: defining a global variable

Hello!

Local variables are available within a single shell instance. If other instances of a shell are invoked by a script, you need to define global variables, those can be inherited by child shells.
For example:

var="0" #defines local variable named var with value 0.

export var #makes variable global.
"Intel inside" is not a label, it's a warning.
Andrew Cowan
Honored Contributor

Re: defining a global variable

If you want a variable to be available to everybody at all times, export it in "/etc/profile".
Muthukumar_5
Honored Contributor

Re: defining a global variable

We can define global variables with login startup scripts, /etc/profile or home directory profile file.

Normal script variables are treated as local for that script.

test="hai" # Local variable

IF you want to include multiple file variables then try as,

-- head.ksh --
# varaiable file
Name="test"
age="23"

#!/bin/ksh
. ./head.ksh

echo $name
echo $age

It will get information from head.ksh as header (include) file and print the data.

HTH.
Easy to suggest when don't know about the problem!
Lawrence Mahan
Frequent Advisor

Re: defining a global variable

In general all shell variable are global within a shell script. However: If you are calling non shell routines within the script then the varables within those routines will be local to that routine. What I meen by a non shell routine is if you are using awk, perl, sql, or other executables that can be called in-line from shell that is not shell. Usually you will have to pass varables to this routines. I.E. awk -v INDATE=${INDATE}
A. Clay Stephenson
Acclaimed Contributor

Re: defining a global variable

Actually, one can have local variables within a shell script. Those defined in functions via the typeset command are local to the function. Run this script and it should make variable scoping clear.

--------------------------------------------
#!/usr/bin/sh

typeset -i I=100 # global
typeset GLOVAR="Global String"

func1()
{
typeset -i I=1 # local definition

echo "I in func1 = ${I}"
echo "GLOVAR in func1 = ${GLOVAR}"
return 0
} # func1

func2()
{
typeset -i I=2 # local definition

echo "I in func2 = ${I}"
echo "GLOVAR in func2 = ${GLOVAR}"
return 0
} # func2

func1
func2
echo "I in main = ${I}"
echo "GLOVAR in main = ${GLOVAR}"
exit 0

----------------------------------------


It's considered good practice to typeset all variables in scripts. Of course, any of these can be exported and made available to child processes.

If it ain't broke, I can fix that.