Terminal Commands: For loop with echo

163,830

Solution 1

The default shell on OS X is bash. You could write this:

for i in {1..100}; do echo http://www.example.com/${i}.jpg; done

Here is a link to the reference manual of bash concerning loop constructs.

Solution 2

for ((i=0; i<=1000; i++)); do
    echo "http://example.com/$i.jpg"
done

Solution 3

Is you are in bash shell:

for i in {1..1000}
do
   echo "Welcome $i times"
done

Solution 4

jot would work too (in bash shell)

for i in `jot 1000 1`; do echo "http://example.com/$i.jpg"; done

Solution 5

you can also use for loop to append or write data to a file. example:

for i in {1..10}; do echo "Hello Linux Terminal"; >> file.txt done

">>" is used to append.

">" is used to write.

Share:
163,830

Related videos on Youtube

Chris
Author by

Chris

Updated on July 05, 2022

Comments

  • Chris
    Chris almost 2 years

    I've never used commands in terminal like this before but I know its possible. How would I for instance write:

    for (int i = 0; i <=1000; i++) {
        echo "http://example.com/%i.jpg",i
    }
    
  • Lenny Markus
    Lenny Markus over 9 years
    Works, but you need to add curly braces around your var in most cases: "foo${i}bar"
  • Admin
    Admin almost 6 years
    I think it should be like this (at least for shell on OS X): for i in {1..10}; do echo "Hello Linux Terminal" >> file.txt; done if you want to write each line to the file or like this: for i in {1..10}; do echo "Hello Linux Terminal"; done >> file.txt if you want to write the total output, all the echo's, in once to the file.