Find the answer to your Linux question:
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... :(...
  1. #1
    Just Joined! vitorgatti's Avatar
    Join Date
    Jan 2008
    Posts
    30

    Question 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... :(

  2. #2
    Linux 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

  3. #3
    Trusted Penguin elija's Avatar
    Join Date
    Jul 2004
    Location
    Either at home or at work or down the pub
    Posts
    2,300
    Try this

    Code:
    #!/bin/bash
    func()
    {
    a = $1
    echo `expr $a + 1`
    }
    
    func(3)
    I have never written a shell script but Googles suggest this might work
    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.

  4. #4
    Just Joined! vitorgatti's Avatar
    Join Date
    Jan 2008
    Posts
    30
    thanks a lot :D

    the script ended like that:

    #!/bin/bash
    func()
    {
    a=$1
    echo `expr $a + 1`
    }

    func 3


    thanks again!

  5. #5
    Linux Guru
    Join Date
    Nov 2007
    Location
    Córdoba (Spain)
    Posts
    1,513
    Quote Originally Posted by vitorgatti View Post
    thanks a lot

    the script ended like that:

    #!/bin/bash
    func()
    {
    a=$1
    echo `expr $a + 1`
    }

    func 3


    thanks again!
    In bash I'd just do this:

    Code:
    #!/bin/bash
    func() {
    echo $(($1 + 1))
    }
     
    func 3

  6. #6
    Trusted Penguin elija's Avatar
    Join Date
    Jul 2004
    Location
    Either at home or at work or down the pub
    Posts
    2,300
    Quote Originally Posted by i92guboj View Post
    In bash I'd just do this:

    Code:
    #!/bin/bash
    func() {
    echo $(($1 + 1))
    }
     
    func 3
    I'm wondering if you can do something like

    $1 = $1 +1

    in a bash script, I am guessing not, but I am at a windows
    computer now so I can't check
    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.

  7. #7
    Linux Guru
    Join Date
    Nov 2007
    Location
    Córdoba (Spain)
    Posts
    1,513
    Quote Originally Posted by elija View Post
    I'm wondering if you can do something like

    $1 = $1 +1

    in a bash script, I am guessing not, but I am at a windows
    computer now so I can't check
    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.

  8. #8
    Linux Enthusiast
    Join Date
    Aug 2006
    Posts
    631
    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.
    An alternative is the C-style:

    Code:
    (($1++))
    Regards

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...