PDA

View Full Version : [SOLVED] very stupid, simple bash script problem - for loop



mcai8sh4
December 6th, 2008, 07:31 PM
Just a little question.
I've been writing bash scripts for quite a while and, whilst I always get there in the end, I usually find little things that don't work when I think they should. I usually just use another method, but his one puzzles me....

Simple example (that doesn't work):

MAX=20
for count in {1..$MAX}; do
...
done

why can't I put a variable in the braces?

Thanks for reading

-s

unutbu
December 6th, 2008, 07:37 PM
#!/bin/bash
MAX=20
for count in $(seq 1 "$MAX"); do
echo "$count"
done

mcai8sh4
December 6th, 2008, 07:39 PM
#!/bin/bash
MAX=20
for count in $(seq 1 "$MAX"); do
echo "$count"
done


ah, thanks for that!

dwhitney67
December 6th, 2008, 07:43 PM
Or:


MAX=20

for (( i = 0; i < $MAX; ++i ))
do
echo -n "$i "
done
echo ""

mcai8sh4
December 6th, 2008, 07:55 PM
Or:


MAX=20

for (( i = 0; i < $MAX; ++i ))
do
echo -n "$i "
done
echo ""


lol - I spent 5+ years using c (and similar) yet I've never used that method in a bash script. Thanks