Results 1 to 8 of 8
can I pass a variable to a function in shell script?
example:
#!/bin/bash
func(a)
{
echo `expr $a + 1`
}
func(3)
this is not working... :(...
- 02-20-2008 #1
shell script doubt (variable pass)
can I pass a variable to a function in shell script?
example:
#!/bin/bash
func(a)
{
echo `expr $a + 1`
}
func(3)
this is not working... :(
- 02-20-2008 #2Linux Enthusiast
- Join Date
- Aug 2006
- Posts
- 631
Here you can find how to use functions with parameters:
http://www.faqs.org/docs/abs/HTML/functions.html
Regards
- 02-20-2008 #3
Try this
I have never written a shell script but Googles suggest this might workCode:#!/bin/bash func() { a = $1 echo `expr $a + 1` } func(3)If we hit that bullseye, the rest of the dominoes will fall like a house of cards. Checkmate! (Zapp Brannigan)
My new blog. It's probably not as good as I think it is.
- 02-20-2008 #4
thanks a lot :D
the script ended like that:
#!/bin/bash
func()
{
a=$1
echo `expr $a + 1`
}
func 3
thanks again!
- 02-20-2008 #5Linux Guru
- Join Date
- Nov 2007
- Location
- Córdoba (Spain)
- Posts
- 1,513
- 02-21-2008 #6If we hit that bullseye, the rest of the dominoes will fall like a house of cards. Checkmate! (Zapp Brannigan)
My new blog. It's probably not as good as I think it is.
- 02-21-2008 #7Linux Guru
- Join Date
- Nov 2007
- Location
- Córdoba (Spain)
- Posts
- 1,513
The closest you can get is that that I posted above. Bash offers some basic arithmetic capabilities when you write the operations in between $((...)), the problem if that they are really limited, and *only* can use integers. So, if you need something more complete, you are bound to use something like bc, which is well known and works well.
- 02-21-2008 #8Linux Enthusiast
- Join Date
- Aug 2006
- Posts
- 631
An alternative is the C-style:The closest you can get is that that I posted above. Bash offers some basic arithmetic capabilities when you write the operations in between $((...)), the problem if that they are really limited, and *only* can use integers. So, if you need something more complete, you are bound to use something like bc, which is well known and works well.
RegardsCode:(($1++))


Reply With Quote
