How to make variables in script function local?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to make variables in script function local?
# 1  
Old 03-06-2008
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
# 2  
Old 03-06-2008
Code:
func1()
{
  typeset var1="asd-func1()"
}

# 3  
Old 03-07-2008
Quote:
Originally Posted by alex_5161
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.

If there are no variable in the function which need to be changed in the calling script call it in a subshell:
Code:
(func1)

Or define the function with parentheses instead of braces:
Code:
func1()
(
   : ... whatever
)

If you are using bash or [d]ash you can declare variables to be local to the function (and its subshells, if any):
Code:
func1()
{
 local var1 var2=$var2
}

Note that this and the typeset command are non-standard (but local really should be made standard).
# 4  
Old 03-07-2008
Quote:
Originally Posted by cfajohnson
Note that this and the typeset command are non-standard (but local really should be made standard).
Sorry, but i beg to differ: "local" is per default an alias to "typeset" and "typeset" is a shell-builtin keyword.

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
# 5  
Old 03-07-2008
Cool!!
Thank you, guys, it's exacly what I was wondering.
And it is good to know how to run script function in subshell. I've thought about that but could not find how.
# 6  
Old 03-07-2008
Quote:
Originally Posted by bakunin
Sorry, but i beg to differ: "local" is per default an alias to "typeset" and "typeset" is a shell-builtin keyword.

It may be in some shells. It is not in bash or [d]ash.
Quote:
In fact it is good style to define every variable with typeset prior to using it.

It is very bad practice to do so as it is not portable.

Code:
$ typeset var
typeset: not found

There is no guarantee that its behaviour will be the same in all shells that do have it.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Function - Make your function return an exit status

Hi All, Good Day, seeking for your assistance on how to not perform my 2nd, 3rd,4th etc.. function if my 1st function is in else condition. #Body function1() { if then echo "exist" else echo "not exist" } #if not exist in function1 my all other function will not proceed.... (4 Replies)
Discussion started by: meister29
4 Replies

2. UNIX for Beginners Questions & Answers

How to have local shell variables in a ksh script seen on remove server in SSH block?

I have googled this and found many solutions, but none of them are working for me. I am in a korn shell, most others reference bsh, maybe that is the issue? Anyway, all I am trying to do is use a variable I have declared in my main script in a remote shell I am running through ssh. So I have a... (8 Replies)
Discussion started by: DJR
8 Replies

3. Shell Programming and Scripting

How to make nested function local?

Hi, If I declare a function inside another function, it overwrites any previously declared function with the same name. This is NOT what I want. Example: #!/bin/bash _test() { echo test; } _myf() { # I'm using the same name as the other function. _test() { echo local test; }... (8 Replies)
Discussion started by: chebarbudo
8 Replies

4. UNIX for Advanced & Expert Users

Script to make a table from Named Variables.

I need a shell script to make a table from Named Variables Input File(Can have multiple lines): a=1,b=2,d=4,e=5 a=11,b=12,c=13,d=14 Output file: a,b,c,d,e 1,2,,4,5 11,12,13,14, Thanks in advance (7 Replies)
Discussion started by: shariramani
7 Replies

5. Programming

Thread function local variables

As I know threads share the memory. But, what about the local variables in the thread function? if i call multiple threads would they allocate seperate local variables for themselves? like thread_func() { int i, j; string... } Are the above local variables defined for each of... (1 Reply)
Discussion started by: saman_glorious
1 Replies

6. UNIX for Advanced & Expert Users

How to make the variables of one script available for the other scripts?

Hello Everyone, I want to know how can we make the variables of one script available for the other script? for example i have three scripts variable_availability.sh,first.sh,second.sh and a file containing variables called common ---------------------------------- cat variable_availability.sh... (2 Replies)
Discussion started by: Kesavan
2 Replies

7. Shell Programming and Scripting

remotely call function from local script

The following code doesn't work properly which means it doesn't displays remote output. #!/bin/ksh #################### Function macAddressFinder ######################## macAddressFinder() { `ifconfig -a > ipInterfaces` `cat ipInterfaces` }... (2 Replies)
Discussion started by: presul
2 Replies

8. Homework & Coursework Questions

I need to make a script that has 4 command line arguments to function properly.

I have no idea what the following means. The teacher is too advanced for me to understand fully. We literally went from running a few commands over the last few months to starting shell scripting. I am not a programmer, I am more hardware oriented. I wish I knew what this question was asking... (3 Replies)
Discussion started by: Wookard
3 Replies

9. Shell Programming and Scripting

passing variables to sed function in a script ....

Hello , I have a script named testscript.sh wherein I have two variables $var and $final (both of which contain a number) I have a sed write function inside this script as follows: sed '1,2 w somefile.txt' fromfile.txt Now , in the above i want to pass $var and $final instead of... (2 Replies)
Discussion started by: shweta_d
2 Replies

10. UNIX for Dummies Questions & Answers

shell script, reading and resetting local variables

Hello, I have a problem with trying to run a shell script that reads in user input, validates, and sets to a 'default' value if the input is not valid. I cannot get the portion of resetting to a default value to work. These lines are skipped, and the value of x is still whatever the user... (1 Reply)
Discussion started by: b888c
1 Replies
Login or Register to Ask a Question