Results 1 to 3 of 3
  1. #1
    Join Date
    Jan 2004
    Posts
    84

    Unanswered: How to pass arguments to a function?

    Hi,
    I have two shell variables $t1 and $t2 which I need to pass to a function in a shell script. The function will do some computation with those two variables and echo the resultant. But I do not know how to pass teh arguments.
    The function written is
    f1()
    {......
    ........
    }

    What should be the syntax to pass arguments $t1 and $t2 to this function.
    Help would be appreciated!

  2. #2
    Join Date
    Jan 2004
    Location
    Germany
    Posts
    167
    may be this example will help you:
    Code:
    #!/bin/bash
    
    function count_from_to()
    {
      i=$1			# i gets value of param1
      while [ $i -le $2 ]	# while param2 greater than i
      do
        echo $i		# print i
        let i=$i+1		# increment i
      done
    }
    
    # main program
    
    read -p "from: " number1	# reading starting number
    read -p "to: " number2		# reading final number
    count_from_to $number1 $number2	# call function passing the two values
    board.perl-community.de - The German Perl-Community

  3. #3
    Join Date
    Jan 2004
    Posts
    84
    Thanks a lot!
    Its done now!

Posting Permissions

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