Jeff Wright
Jeff Wright

Reputation: 1194

Why does my bash script print colon at end of loop?

I have the following bash script:

#!/bin/sh

num_loops=3

for i in `seq $num_loops`:
do
    printf 'Iteration %s\n' $i
done

And when I run it, I get the following output.

$ ./loop-test.sh
Iteration 1
Iteration 2
Iteration 3:

I'm wondering why the script generates an extraneous colon (":") at the end of the final iteration? I know that the variable 'i' is a string, but why would the last iteration in the 'seq' command append the colon?

Upvotes: 5

Views: 1698

Answers (1)

Marc B
Marc B

Reputation: 360632

Because you're telling it to use one:

for i in `seq $num_loops`:
                         ^---

The : is not necessary, and becomes part of the command line arguments:

for i in 1 2 3:

at the final iteration, $i = '3:', basically.

Upvotes: 11

Related Questions