[Techtalk] Bash functions with user-defined parameters

Liz Young liz at kandew.net
Tue Apr 29 09:43:45 EST 2003


Hi Berenice,

On Monday 28 April 2003 5:52 am, Berenice wrote:
> #!/bin/bash
> myfunction()
> {
> while [ $1 -le $2 ]
> do
>  echo $1
>  #problem here...$(( $1 + 1 ))
> done
> }
>
>
> The problem is with the $(( $1 + 1 )) because you can't do $1=$(( $1
> + 1 )).  Is there a way to fix this, or should I forget about using
> $1 and $2?

I got it working like this:

myfunction()
{
num1=$1
while [ $num1 -le $2 ]
do
echo $num1
let num1=num1+1
done
}

liz:~> myfunction 1 4
1
2
3
4

Thank you for posting your question, now I know a little more about bash 
scripting! :-)

bash --version
GNU bash, version 2.05.0(1)-release (i386-suse-linux)
Copyright 2000 Free Software Foundation, Inc.

Liz


More information about the Techtalk mailing list