1755095 Members
1368 Online
108829 Solutions
New Discussion юеВ

ksh scripting concepts

 
stormmykL
Valued Contributor

ksh scripting concepts

Hi,

i'm not sure if i'm in the right forum.. i'm on my 6th day in studying Unix Shell (specifically ksh scripting) and i need help about some concept that i cannot specifically find over the net (maybe i should say this - my background is Visual Basic scripting):

- what's the concept of variable settings for:
1. main script '(do variables here become "global" and can be used within called functions?)
2. functions '(do variables created here retain its values once script has exited from the function?)
3. (any sub procedures in ksh?)
4. can we create "Environment" variables in ksh? (i mean variables that are created anywhere but can be used throughout the whole script)

please itemize your replies.. it means a lot.. thanks!

~michael
2 REPLIES 2
James R. Ferguson
Acclaimed Contributor

Re: ksh scripting concepts

Hi Michael:

1. Variables are global in scope and can be used (updated and/or referenced in all functions (subroutines).

2. Variables declared in the scope of a subroutine are actually added to the global namespace unless they are declared with 'typeset' in which case they are local to the subroutine and "disappear" (or are restored to their global state) once the subroutine is exited.

For example, consider:

#!/bin/ksh
X=michael
function who
{
echo "my last 'Y' was '${Y}'"
typeset Y=somebody
Z=whoever #...no typeset...
echo "my global value is '${X}'"
typeset X=someone_else
echo "my local value is '${X}'"
echo "I am '${Y}'"
}
who
echo "'${X}' and '${Y}' and '${Z}'"
exit 0

3. I'm not sure what you mean by "sub procedures".

4. Environmental variables are variables that are inherited from a process's parent. A child process can use and modify an environmental variable but any modification is not propagated back to its parent. See #1, above.

Regards!

...JRF...

Dennis Handly
Acclaimed Contributor

Re: ksh scripting concepts

>3. any sub procedures in ksh?

You can have aliases. And functions in other files through FPATH.