![]() |
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here. |
More UNIX and Linux Forum Topics You Might Find Helpful
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| MAKE and its macros and variables | JerryHone | UNIX for Dummies Questions & Answers | 1 | 04-19-2008 11:23 AM |
| Problem with global and local variables | qzv2jm | Shell Programming and Scripting | 2 | 03-04-2008 01:18 PM |
| passing variables to sed function in a script .... | shweta_d | Shell Programming and Scripting | 2 | 06-05-2007 07:13 AM |
| rsh with local variables | jo_aze | UNIX for Dummies Questions & Answers | 1 | 10-07-2002 10:54 AM |
| shell script, reading and resetting local variables | b888c | UNIX for Dummies Questions & Answers | 1 | 08-20-2001 05:52 PM |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
||||
|
How to make variables in script function local?
Is it possible to make function variables local? I mean for example, I have a script variable 'name' and in function I have declared variable 'name' I need to have script's 'name' have the same value as it was before calling the function with the same declaration. The way to preserve a script var in begining of the function and restore before exiting is last one. I am looking for something more intelligent. Why? I have situation, when I need to consolidate many script files into one. Many function in result file use the same variables and call each other. It was not a problem before as those functions ran in different process, now -the same. Example: Code:
cat > tst.sh
#! /bin/bash
func1()
{
var1="asd-func1()"
}
func2()
{
#! /bin/bash
var1="lkj-func2()"
}
var1=main_body
echo $var1
func1;
echo "after func1: $var1"
func2;
echo "after func2: $var1"
^C
> tst.sh
main_body
after func1: asd-func1()
after func2: lkj-func2()
>
Thanks Last edited by alex_5161; 03-06-2008 at 08:07 PM.. Reason: add result |
|
||||
|
Quote:
In fact it is good style to define every variable with typeset prior to using it. Even if it is not necessary it makes the code more reliable (by eliminating possible sources of side effects) and better readable too. bakunin |
|
|||||
|
Quote:
Quote:
There is no guarantee that its behaviour will be the same in all shells that do have it. |
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|