How to call variable inside a function globally?

 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers How to call variable inside a function globally?
# 1  
Old 01-16-2017
How to call variable inside a function globally?

Hi Gurus,

Is there a way to call a variable inside a function anywhere within the script?

Thanks.

BR,
Ernesto
# 2  
Old 01-16-2017
You need a variable with global scope:

Code:
#!/bin/bash
# this variable, foo, has global scope
# it has to be defined in the code before the function
foo=1

func()
{
   a=$(( foo +1 ))
   foo=$a
}

# main code block
echo "before function foo = $foo"
func
echo "after function foo = $foo"
exit

This User Gave Thanks to jim mcnamara For This Post:
# 3  
Old 01-16-2017
Jim, why didn't you start from 0 ? Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Need help on awk for printing the function name inside each function

Hi, I am having script which contains many functions. Need to print each function name at the starting of the function. Like below, functionname() { echo "functionname" commands.... } I've tried like below, func=`grep "()" scriptname | cut -d "(" -f1` for i in $func do nawk -v... (4 Replies)
Discussion started by: Sumanthsv
4 Replies

2. Shell Programming and Scripting

Dereferencing variable inside egrep call

Hi guys I am trying to dereference a variable inside 'egrep -v ' command and getting a 'egrep: syntax error' : $ echo $exclude_list ts584d hf584db for i in `echo $exclude_list`; do egrep -v ${i} my_file done egrep: syntax error egrep: syntax error The syntax of the loop is correct.... (1 Reply)
Discussion started by: aoussenko
1 Replies

3. Shell Programming and Scripting

Call function inside CASE

i have a case statement which branches to different sections based on an input. Each branch needs to call a function. below is the code. FOr some reason, the code inside the function is not getting executed. the code is below for reference. in the below code echo "Function 1" which is there... (2 Replies)
Discussion started by: cvsanthosh
2 Replies

4. Shell Programming and Scripting

Error during Accessing Global variable inside function

emailid=myemail@xyz.com taskName="DB-Backup" starttime=`date` email() { subject="$taskName" ": " $* " at `date` " mutt -s "$subject" $emailid < /dev/null } email "Starting" #do my stuff email "Finished" The above code gives following error ./dbbackup.sh: line 6: :... (5 Replies)
Discussion started by: nitiraj.rathore
5 Replies

5. Shell Programming and Scripting

After exit from function it should not call other function

Below is my script that is function properly per my conditions but I am facing one problem here that is when one function fails then Iy should not check other functions but it calls the other function too So anyone can help me how could i achieve this? iNOUT i AM GIVING TO THE... (1 Reply)
Discussion started by: rohit22hamirpur
1 Replies

6. Shell Programming and Scripting

How to declare a variable which can be accessed globally

Hi I've few shell scripts which are responsible for triggering the continuous builds for a specific module. Each shell script is for a Module. Shell script has some module specific settings in the beginning and then it triggers the builds (which are nothing but some combination of Java programs... (2 Replies)
Discussion started by: kgsrinivas
2 Replies

7. Shell Programming and Scripting

variable inside variable inside loop headache

Hi Gurus I have a file called /tmp/CMDB which looks like this serial: 0623AN1208 hostname: server1 model: x4100 assetID: 1234 I am writing a for loop that will go through this file line by line creating a variable of itself. Using the first iteration of the loop (i.e. the first line) as... (6 Replies)
Discussion started by: hcclnoodles
6 Replies

8. Infrastructure Monitoring

diffrence between method call and function call in perl

Hello, I have a problem with package and name space. require "/Mehran/DSGateEngineLib/general.pl"; use strict; sub System_Status_Main_Service_Status_Intrusion_Prevention { my %idpstatus; my @result; &General_ReadHash("/var/dsg/idp/settings",\%idpstatus); #print... (4 Replies)
Discussion started by: Zaxon
4 Replies

9. Shell Programming and Scripting

Bash: how to call function having it's name in variable?

Hello. Looking for a method of modularizing my bash script, I am stuck with such a problem. For example, I have: MODULE_NAME="test" FUNCTION_NAME="run" How do I can a function with name test_run? (4 Replies)
Discussion started by: FractalizeR
4 Replies

10. UNIX for Dummies Questions & Answers

passing a variable inside a variable to a function

I would like to know how to pass a variable inside a variable to a function. sample code below -------------- for x in 1 9 do check_null $C$x ##call function to check if the value is null if then echo "line number:$var_cnt,... (2 Replies)
Discussion started by: KingVikram
2 Replies
Login or Register to Ask a Question