bash function variable questions

Ok here is the problem

declare -i number=10

number=10

fuction addtonumber{
echo $number  # number=10
let numbet+=1
echo $number #number =11
}

echo $number #number =10
addtonumber
echo $number #number=10   <------ THIS IS THE PROBLEM IT SHOULD BE 11 AFTER THE FUNCTION ADDS ONE TO IT

My actual situation is a little more complicates as I am piping standard input from sort into the function.
I don't really want to pass parameter to the functions - actually I tried that and I had a little trouble with it

Any suggetions?

Thanks
jculkincys







LVL 2
jculkincysAsked:
Who is Participating?
 
ahoffmannCommented:
a function in bash (most ksh too) is something like a subshell with its own namespace, hence you cannot manipulate other (in particular global) variables there

you have do do something like:

function incr {
let x=$1+1
echo $x
}
x=`incr $x`;
0
 
guruyayaCommented:
first, notice this litle mistake
let numbet+=1
              ^

You`ve made this error here, you could have made it in your script.

Yet, if it`s not that, could be that you need to write  export number, after declaring it, though it worked for me without this change.
Btw - Have you declared #!/bin/bash?

0
 
jculkincysAuthor Commented:
Hello guruyaya

Yes I have declared #!/bin/bash

I doubled checked and I didn't have an actual spelling mistake in my script - tha t would have been nice though

I will try export

Do you think this could be an issue of Bash versions acting differently?

I am using BASH 2.05

jculkincys
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
jculkincysAuthor Commented:
Hmm export doesn't seem to work
0
 
jculkincysAuthor Commented:


fuction addtonumber{
declare -i number=10
echo $number  # number=10
let numbet+=1
echo $number #number =11
}

addtonumber
echo $number #number=10  <---- this produces the error "unbound variable"

I was under the impression that unless you specify "local" all variable are declared globally

This is leading me to belive that its a problem with my version of bash
0
 
jculkincysAuthor Commented:
I typed very bad in that example

here is that example that I tested

#!/bin/bash
fuction addtonumber{
declare -i number=10
echo $number  # number=10
let numbet+=1
echo $number #number =11
}

addtonumber
echo $number #number=10    #<----in Bash 3.1.11 I get number =11 in Bash 2.05 I get 11 as well so its not a version problem

Things that make you go Hmmmmmm......

0
 
jculkincysAuthor Commented:
Alright I finally recreated the problem in a useful manner for others

#!/bin/bash

declare -i number=10

function addtonumber {
echo "Value after entering function: $number"
pushd /bin
let number+=1
popd
echo "Value after addition in function: $number"
}

echo "Value before function: $number"
echo "TEST" | addtonumber
echo "Value after functin: $number"  #<----- number will =10 but I want it to be 11
0
 
ahoffmannCommented:
BTW,
x=`expr $x '+' 1`

# works in any shell ;-)
0
 
jculkincysAuthor Commented:
I am not sure I understand
I think its something to do with my piping to the fuction because the following script works correctly (and it does not pipe to the function)

#!/bin/bash

declare -i number=10

function addtonumber {
echo "Value after entering function: $number"
let number+=1
echo "Value after addition in function: $number"
}

echo "Value before function: $number"
addtonumber
echo "Value after functin: $number"  #<-----number will be 11



It seems from this example that a function change the value of a global variable
0
 
ahoffmannCommented:
the only difference to your initial question is that you miss

number=10

could that be the problem?
0
 
jculkincysAuthor Commented:
I just tested it the last script including number=10 and had no luck
0
 
xDamoxCommented:
Hi,

This works fine:


#!/bin/bash

export number=10

function addtonumber(){
echo "Inside: " $number
let "number += 1"
echo "Inside: " $number
}

echo $number
addtonumber
echo $number


The output form the above I get:

10
Inside:  10
Inside:  11
11

:D I noticed your let was not in quotes :)
0
 
jculkincysAuthor Commented:
Thanks for joining xDamonX

Here is where I have the problem though

#!/bin/bash

export number=10

function addtonumber(){
echo "Inside: " $number
let "number += 1"
echo "Inside: " $number
}

echo $number
echo "Test" | addtonumber  # <------ Pipe to function creates the problem
echo $number



The output form the above I get:

10
Inside:  10
Inside:  11
10


I know the pipe plays no role in example function but it does in my script

0
 
xDamoxCommented:
Hi,

How come your using pipe you can have the following:





#!/bin/bash

export number=10

function addtonumber(){
echo "Inside: " $number " going to add $1 to $number"
let "number += $1"
echo "Inside: " $number
echo "The number sent over is: " $1
}

echo $number
addtonumber 1  # send one over to the function
echo $number


0
 
jculkincysAuthor Commented:
I'm sorry xDamonx I don't think I described the problem well enough

I really didn't need to do the pipe in the example but by real script needed it for something else
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.