Terminal Commands: For loop with echo

MacosTerminalCommand

Macos Problem Overview


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
}

Macos Solutions


Solution 1 - Macos

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 - Macos

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

Solution 3 - Macos

Is you are in bash shell:

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

Solution 4 - Macos

jot would work too (in bash shell)

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

Solution 5 - Macos

By using jot:

jot -w "http://example.com/%d.jpg" 1000 1

Solution 6 - Macos

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionChrisView Question on Stackoverflow
Solution 1 - MacosSimonView Answer on Stackoverflow
Solution 2 - MacosGordon DavissonView Answer on Stackoverflow
Solution 3 - MacosCygnusx1View Answer on Stackoverflow
Solution 4 - MacosthomasView Answer on Stackoverflow
Solution 5 - MacosGrimmaceView Answer on Stackoverflow
Solution 6 - MacosvishalView Answer on Stackoverflow