Operating System - HP-UX
1752793 Members
6220 Online
108789 Solutions
New Discussion юеВ

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!