The UNIX and Linux Forums  


Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



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

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 03-06-2008
alex_5161 alex_5161 is offline
Registered User
  
 

Join Date: Jan 2007
Location: Detroit
Posts: 124
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 (permalink)  
Old 03-06-2008
vgersh99's Avatar
vgersh99 vgersh99 is online now Forum Staff  
Moderator
  
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 5,131

Code:
func1()
{
  typeset var1="asd-func1()"
}

  #3 (permalink)  
Old 03-07-2008
cfajohnson's Avatar
cfajohnson cfajohnson is offline Forum Advisor  
Shell programmer, author
  
 

Join Date: Mar 2007
Location: Toronto, Canada
Posts: 2,365
Quote:
Originally Posted by alex_5161 View Post
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 (permalink)  
Old 03-07-2008
bakunin bakunin is offline Forum Staff  
Bughunter Extraordinaire
  
 

Join Date: May 2005
Location: In the leftmost byte of /dev/kmem
Posts: 1,633
Quote:
Originally Posted by cfajohnson View Post
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 (permalink)  
Old 03-07-2008
alex_5161 alex_5161 is offline
Registered User
  
 

Join Date: Jan 2007
Location: Detroit
Posts: 124
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 (permalink)  
Old 03-07-2008
cfajohnson's Avatar
cfajohnson cfajohnson is offline Forum Advisor  
Shell programmer, author
  
 

Join Date: Mar 2007
Location: Toronto, Canada
Posts: 2,365
Quote:
Originally Posted by bakunin View Post
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.
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 03:57 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0