3 add one more tag
source | link

How can youto pass parameters to function in a bash script?

I'd like to write a function that I can call from a script with many different variables. For some reasons I'm having a lot of trouble doing this. Examples I've read always just use a global variable but that wouldn't make my code much more readable as far as I can see.

Intended usage example:

#!/bin/bash
#myscript.sh
var1=$1
var2=$2
var3=$3
var4=$4

add(){
result=$para1 + $para2
}

add $var1 $var2
add $var3 $var4
# end of the script

./myscript.sh 1 2 3 4
#!/bin/bash
#myscript.sh
var1=$1
var2=$2
var3=$3
var4=$4

add(){
result=$para1 + $para2
}

add $var1 $var2
add $var3 $var4
# end of the script

./myscript.sh 1 2 3 4

I tried using $1$1 and such in the function, but then it just takes the global one the whole script was called from. Basically what I'm looking for is something like $1$1, $2$2 and so on but in the local context of a function. Like you know, functions work in any proper language.

I'd be happy about any suggestions.

How can you pass parameters to function in a script?

I'd like to write a function that I can call from a script with many different variables. For some reasons I'm having a lot of trouble doing this. Examples I've read always just use a global variable but that wouldn't make my code much more readable as far as I can see.

Intended usage example:

#!/bin/bash
#myscript.sh
var1=$1
var2=$2
var3=$3
var4=$4

add(){
result=$para1 + $para2
}

add $var1 $var2
add $var3 $var4
# end of the script

./myscript.sh 1 2 3 4

I tried using $1 and such in the function, but then it just takes the global one the whole script was called from. Basically what I'm looking for is something like $1, $2 and so on but in the local context of a function. Like you know, functions work in any proper language.

I'd be happy about any suggestions.

How to pass parameters to function in a bash script?

I'd like to write a function that I can call from a script with many different variables. For some reasons I'm having a lot of trouble doing this. Examples I've read always just use a global variable but that wouldn't make my code much more readable as far as I can see.

Intended usage example:

#!/bin/bash
#myscript.sh
var1=$1
var2=$2
var3=$3
var4=$4

add(){
result=$para1 + $para2
}

add $var1 $var2
add $var3 $var4
# end of the script

./myscript.sh 1 2 3 4

I tried using $1 and such in the function, but then it just takes the global one the whole script was called from. Basically what I'm looking for is something like $1, $2 and so on but in the local context of a function. Like you know, functions work in any proper language.

2 added 79 characters in body
source | link

I'd like to write a function that I can call from a script with many different variables. For some reasons I'm having a lot of trouble doing this. Examples I've read always just use a global variable but that wouldn't make my code much more readable as far as I can see.

Intended usage example:

var1=1#!/bin/bash
var2=2#myscript.sh
var3=3var1=$1
var4=4var2=$2
var3=$3
var4=$4

add(){
result=$para1 + $para2
}

add $var1 $var2
add $var3 $var4
# end of the script

./myscript.sh 1 2 3 4

I tried using $1 and such in the function, but then it just takes the global one the whole script was called from. Basically what I'm looking for is something like $1, $2 and so on but in the local context of a function. Like you know, functions work in any proper language.

I'd be happy about any suggestions.

I'd like to write a function that I can call from a script with many different variables. For some reasons I'm having a lot of trouble doing this. Examples I've read always just use a global variable but that wouldn't make my code much more readable as far as I can see.

Intended usage example:

var1=1
var2=2
var3=3
var4=4

add(){
result=$para1 + $para2
}

add $var1 $var2
add $var3 $var4

I tried using $1 and such, but then it just takes the global one the whole script was called from. Basically what I'm looking for is something like $1, $2 and so on but in the local context of a function. Like you know, functions work in any proper language.

I'd be happy about any suggestions.

I'd like to write a function that I can call from a script with many different variables. For some reasons I'm having a lot of trouble doing this. Examples I've read always just use a global variable but that wouldn't make my code much more readable as far as I can see.

Intended usage example:

#!/bin/bash
#myscript.sh
var1=$1
var2=$2
var3=$3
var4=$4

add(){
result=$para1 + $para2
}

add $var1 $var2
add $var3 $var4
# end of the script

./myscript.sh 1 2 3 4

I tried using $1 and such in the function, but then it just takes the global one the whole script was called from. Basically what I'm looking for is something like $1, $2 and so on but in the local context of a function. Like you know, functions work in any proper language.

I'd be happy about any suggestions.

1
source | link

How can you pass parameters to function in a script?

I'd like to write a function that I can call from a script with many different variables. For some reasons I'm having a lot of trouble doing this. Examples I've read always just use a global variable but that wouldn't make my code much more readable as far as I can see.

Intended usage example:

var1=1
var2=2
var3=3
var4=4

add(){
result=$para1 + $para2
}

add $var1 $var2
add $var3 $var4

I tried using $1 and such, but then it just takes the global one the whole script was called from. Basically what I'm looking for is something like $1, $2 and so on but in the local context of a function. Like you know, functions work in any proper language.

I'd be happy about any suggestions.