Hi Team,
i need to repeat a function n number of times, whereas 1..n is a value which should be inside a same function, how to exactly repeat n number of times and each value shld get subsitiuted for each and every fn
repeat(){
for i in {1.."$n"}
do
echo "$n"
done
exit 0
}
read -p "Enter the value of n:" n
repeat
-------------------------------------------------------------------
n value i will give , so that n = 5
then i shold get o/p
1
2
3
4
5
repeat functions in shell script 2
Re: repeat functions in shell script 2
Look out: Bash brace expansion takes place before variable expansion. In this case 1.."$n" will be interpreted as a single string *before* variable is expanded. Conclusion: i will only loop through one single string element -> this is not what you want.for i in {1.."$n"}
Instead you're probably looking for something like this:
Code: Select all
repeat() {
i=1
while [ "$i" -le "$1" ]; do
echo "$i";
i=$(($i + 1))
done
}
read -p "Enter the value of n:" n
repeat $n