🕷️ Crawler Inspector

URL Lookup

Direct Parameter Lookup

Raw Queries and Responses

1. Shard Calculation

Query:
Response:
Calculated Shard: 169 (from laksa060)

2. Crawled Status Check

Query:
Response:

3. Robots.txt Check

Query:
Response:

4. Spam/Ban Check

Query:
Response:

5. Seen Status Check

ℹ️ Skipped - page is already crawled

đź“„
INDEXABLE
âś…
CRAWLED
16 days ago
🤖
ROBOTS ALLOWED

Page Info Filters

FilterStatusConditionDetails
HTTP statusPASSdownload_http_code = 200HTTP 200
Age cutoffPASSdownload_stamp > now() - 6 MONTH0.6 months ago
History dropPASSisNull(history_drop_reason)No drop reason
Spam/banPASSfh_dont_index != 1 AND ml_spam_score = 0ml_spam_score=0
CanonicalPASSmeta_canonical IS NULL OR = '' OR = src_unparsedNot set

Page Details

PropertyValue
URLhttps://stackoverflow.com/questions/1445452/shell-script-for-loop-syntax
Last Crawled2026-04-08 21:13:51 (16 days ago)
First Indexednot set
HTTP Status Code200
Content
Meta Titleunix - Shell script "for" loop syntax - Stack Overflow
Meta Descriptionnull
Meta Canonicalnull
Boilerpipe Text
This question shows research effort; it is useful and clear 279 Save this question. Show activity on this post. I have gotten the following to work: for i in {2..10} do echo "output: $i " done It produces a bunch of lines of output: 2 , output: 3 , so on. However, trying to run the following: max=10 for i in {2.. $max } do echo " $i " done produces the following: output: {2..10} How can I get the compiler to realize it should treat $max as the other end of the array, and not part of a string? asked Sep 18, 2009 at 15:57 6 This answer is useful 327 Save this answer. Show activity on this post. Brace expansion, {x..y} is performed before other expansions, so you cannot use that for variable length sequences. Instead, use the seq 2 $max method as user mob stated . So, for your example it would be: max=10 for i in ` seq 2 $max ` do echo " $i " done answered Sep 18, 2009 at 16:06 3 Comments There is no good reason to use an external command such as seq to count and increment numbers in the for loop, hence it is recommend that you avoid using seq . This command is left for compatibility with old bash. The built-in commands are fast enough. for (( EXP1; EXP2; EXP3 )) ... 2016-07-25T09:27:01.267Z+00:00 @miller the for (( ... )) syntax isn't POSIX and that means for example that it won't work out of the box on things like Alpine Linux. seq does. 2016-09-30T22:21:49.397Z+00:00 @Wernight Not just Alpine Linux; #!/bin/sh is Dash in Debian/Ubuntu. 2019-03-12T20:50:46.243Z+00:00 This answer is useful 102 Save this answer. Show activity on this post. Try the arithmetic-expression version of for : max=10 for (( i= 2 ; i <= $max ; ++i )) do echo " $i " done This is available in most versions of bash, and should be Bourne shell (sh) compatible also. answered Sep 18, 2009 at 16:18 system PAUSE 38.9k 21 gold badges 65 silver badges 59 bronze badges 2 Comments @Flow: Hm, I just tried it on a couple of systems (Linux and BSD based) with #!/bin/sh and it worked fine. Invoked under bash and specifically under /bin/sh, still worked. Maybe the version of sh matters? Are you on some old Unix? 2012-03-26T21:29:02.433Z+00:00 Very few systems have a dedicated sh , instead making it a link to other another shell. Ideally, such a shell invoked as sh would only support those features in the POSIX standard, but by default let some of their extra features through. The C-style for-loop is not a POSIX feature, but may be in sh mode by the actual shell. 2013-09-12T19:41:22.943Z+00:00 This answer is useful 40 Save this answer. Show activity on this post. Step the loop manually: i=0 max=10 while [ $i -lt $max ] do echo "output: $i" true $(( i++ )) done If you don’t have to be totally POSIX, you can use the arithmetic for loop: max=10 for (( i=0; i < max; i++ )); do echo "output: $i"; done Or use jot (1) on BSD systems: for i in $( jot 0 10 ); do echo "output: $i"; done answered Sep 18, 2009 at 16:17 Nietzche-jou 14.8k 4 gold badges 37 silver badges 46 bronze badges 3 Comments true $(( i++ )) doesn't work in all cases, so most portable would be true $((i=i+1)) . 2012-03-22T14:50:01.037Z+00:00 semi colon should not come in "for (( i=0; i < max; i++ ));" 2014-02-10T13:02:15.42Z+00:00 jot 0 10 might be an infinite loop starting at 10. To get the same effect as max=10; for (( i=0; i < max; i++ )); do echo "output: $i"; done I think the syntax should be jot -w 'output: %d' 10 0 (directly print out sans loop) or just for i in $( jot 10 0 ); do ….. for the looped variant 2024-05-26T01:20:19.787Z+00:00 This answer is useful 22 Save this answer. Show activity on this post. If the seq command available on your system: for i in ` seq 2 $max ` do echo "output: $i " done If not, then use poor man's seq with perl : seq =`perl -e "\$,=' ';print 2.. $max " ` for i in $seq do echo "output: $i " done Watch those quote marks. answered Sep 18, 2009 at 16:01 mob 119k 18 gold badges 159 silver badges 291 bronze badges Comments This answer is useful 19 Save this answer. Show activity on this post. We can iterate loop like as C programming. #!/bin/bash for ((i= 1 ; i<= 20 ; i=i+ 1 )) do echo $i done answered Jul 21, 2018 at 12:07 rashedcs 3,763 2 gold badges 43 silver badges 44 bronze badges Comments This answer is useful 10 Save this answer. Show activity on this post. There's more than one way to do it. max=10 for i in ` eval "echo {2.. $max }" ` do echo " $i " done answered Sep 18, 2009 at 16:20 ephemient 207k 40 gold badges 293 silver badges 404 bronze badges 2 Comments A security risk, since eval will evaluate anything you set max to. Consider max="2}; echo ha; #" , then replace echo ha with something more destructive. 2013-09-12T19:45:27.713Z+00:00 (S)he set max to 10. No risk. 2014-04-21T01:13:16.847Z+00:00 This answer is useful 5 Save this answer. Show activity on this post. This is a way: Bash: max=10 for i in $(bash -c "echo {2.. ${max} }" ); do echo $i ; done The above Bash way will work for ksh and zsh too, when bash -c is replaced with ksh -c or zsh -c respectively. Note: for i in {2..${max}}; do echo $i; done works in zsh and ksh . answered Jul 12, 2015 at 12:30 Jahid 22.6k 10 gold badges 97 silver badges 114 bronze badges Comments This answer is useful 3 Save this answer. Show activity on this post. Well, as I didn't have the seq command installed on my system (Mac OS X v10.6.1 (Snow Leopard)), I ended up using a while loop instead: max=5 i=1 while [ $max -gt $i ] do (stuff) done * Shrugs * Whatever works. answered Sep 18, 2009 at 16:16 eykanal 27.3k 19 gold badges 86 silver badges 114 bronze badges 1 Comment seq is relatively new. I only found out about it a few months ago. But you can use a 'for' loop!! The disadvantage of a 'while' is that you have to remember to increment the counter somewhere inside the loop, or else loop downwards. 2009-09-18T16:23:03.563Z+00:00 This answer is useful 2 Save this answer. Show activity on this post. These all do {1..8} and should all be POSIX. They also will not break if you put a conditional continue in the loop. The canonical way: f= while [ $((f+= 1 )) -le 8 ] do echo $f done Another way: g= while g= ${g} 1 [ ${#g} -le 8 ] do echo ${#g} done and another: set -- while set $* . [ ${#} -le 8 ] do echo ${#} done answered Dec 19, 2015 at 18:51 user4427511 1 Comment Wonderful, I'm looking for a solution for busybox's ash, and your answer is perfect 2023-01-27T19:57:03.243Z+00:00 This answer is useful 2 Save this answer. Show activity on this post. Here it worked on Mac OS X. It includes the example of a BSD date, how to increment and decrement the date also: for ((i= 28 ; i>= 6 ; i--)); do dat=` date -v- ${i} d -j "+%Y%m%d" ` echo $dat done answered Dec 6, 2013 at 5:42 minhas23 9,761 6 gold badges 60 silver badges 40 bronze badges Comments This answer is useful 1 Save this answer. Show activity on this post. Use: max=10 for i in ` eval echo {2.. $max }` do echo $i done You need the explicit 'eval' call to reevaluate the {} after variable substitution. answered Sep 18, 2009 at 16:27 Comments This answer is useful 0 Save this answer. Show activity on this post. Use seq 2 10 | xargs printf 'output: %d\n' , if you only need the output, and the loop may be implicit. answered Dec 15, 2025 at 0:47 Jens Jensen 1,128 1 gold badge 10 silver badges 22 bronze badges Comments Start asking to get answers Find the answer to your question by asking. Ask question Explore related questions See similar questions with these tags.
Markdown
# ![site logo](https://stackoverflow.com/Content/Img/SE-logo75.png) By clicking “Sign up”, you agree to our [terms of service](https://stackoverflow.com/legal/terms-of-service/public) and acknowledge you have read our [privacy policy](https://stackoverflow.com/legal/privacy-policy). # OR Already have an account? [Log in](https://stackoverflow.com/users/login) [Skip to main content](https://stackoverflow.com/questions/1445452/shell-script-for-loop-syntax#content) [Stack Overflow](https://stackoverflow.com/) 1. [About](https://stackoverflow.co/) 2. Products 3. [For Teams](https://stackoverflow.co/internal/) 1. [Stack Internal Implement a knowledge platform layer to power your enterprise and AI tools.](https://stackoverflow.co/internal/?utm_medium=referral&utm_source=stackoverflow-community&utm_campaign=top-nav&utm_content=stack-overflow-for-teams) 2. [Stack Data Licensing Get access to top-class technical expertise with trusted & attributed content.](https://stackoverflow.co/data-licensing/?utm_medium=referral&utm_source=stackoverflow-community&utm_campaign=top-nav&utm_content=overflow-api) 3. [Stack Ads Connect your brand to the world’s most trusted technologist communities.](https://stackoverflow.co/advertising/?utm_medium=referral&utm_source=stackoverflow-community&utm_campaign=top-nav&utm_content=stack-overflow-advertising) 4. [Releases Keep up-to-date on features we add to Stack Overflow and Stack Internal.](https://stackoverflow.blog/releases/?utm_medium=referral&utm_source=stackoverflow-community&utm_campaign=top-nav&utm_content=releases) 5. [About the company](https://stackoverflow.co/?utm_medium=referral&utm_source=stackoverflow-community&utm_campaign=top-nav&utm_content=about-the-company) [Visit the blog](https://stackoverflow.blog/?utm_medium=referral&utm_source=stackoverflow-community&utm_campaign=top-nav&utm_content=blog) 1. ### [current community](https://stackoverflow.com/) - [Stack Overflow](https://stackoverflow.com/) [help](https://stackoverflow.com/help) [chat](https://chat.stackoverflow.com/?tab=explore) - [Meta Stack Overflow](https://meta.stackoverflow.com/) ### your communities [Sign up](https://stackoverflow.com/users/signup?ssrc=site_switcher&returnurl=https%3A%2F%2Fstackoverflow.com%2Fquestions%2F1445452%2Fshell-script-for-loop-syntax) or [log in](https://stackoverflow.com/users/login?ssrc=site_switcher&returnurl=https%3A%2F%2Fstackoverflow.com%2Fquestions%2F1445452%2Fshell-script-for-loop-syntax) to customize your list. ### [more stack exchange communities](https://stackexchange.com/sites) [company blog](https://stackoverflow.blog/) 2. [Log in](https://stackoverflow.com/users/login?ssrc=head&returnurl=https%3A%2F%2Fstackoverflow.com%2Fquestions%2F1445452%2Fshell-script-for-loop-syntax) 3. [Sign up](https://stackoverflow.com/users/signup?ssrc=head&returnurl=https%3A%2F%2Fstackoverflow.com%2Fquestions%2F1445452%2Fshell-script-for-loop-syntax) # Let's set up your homepage Select a few topics you're interested in: python javascript c\# reactjs java android html flutter c++ node.js typescript css r php angular next.js spring-boot machine-learning sql excel ios azure docker Or search from our full list: - javascript - python - java - c\# - php - android - html - jquery - c++ - css - ios - sql - mysql - r - reactjs - node.js - arrays - c - asp.net - json - python-3.x - .net - ruby-on-rails - sql-server - swift - django - angular - objective-c - excel - pandas - angularjs - regex - typescript - ruby - linux - ajax - iphone - vba - xml - laravel - spring - asp.net-mvc - database - wordpress - string - flutter - postgresql - mongodb - wpf - windows - xcode - amazon-web-services - bash - git - oracle-database - spring-boot - dataframe - azure - firebase - list - multithreading - docker - vb.net - react-native - eclipse - algorithm - powershell - macos - visual-studio - numpy - image - forms - scala - function - vue.js - performance - twitter-bootstrap - selenium - winforms - kotlin - loops - dart - express - sqlite - hibernate - matlab - python-2.7 - shell - rest - apache - entity-framework - android-studio - csv - maven - linq - qt - dictionary - unit-testing - asp.net-core - facebook - apache-spark - tensorflow - file - swing - class - unity-game-engine - sorting - date - authentication - go - symfony - t-sql - opencv - matplotlib - .htaccess - google-chrome - for-loop - datetime - codeigniter - perl - http - validation - sockets - google-maps - object - uitableview - xaml - oop - visual-studio-code - if-statement - cordova - ubuntu - web-services - email - android-layout - github - spring-mvc - elasticsearch - kubernetes - selenium-webdriver - ms-access - ggplot2 - user-interface - parsing - pointers - google-sheets - c++11 - security - machine-learning - google-apps-script - ruby-on-rails-3 - templates - flask - nginx - variables - exception - sql-server-2008 - gradle - debugging - tkinter - delphi - listview - jpa - asynchronous - haskell - web-scraping - jsp - pdf - ssl - amazon-s3 - google-cloud-platform - testing - xamarin - jenkins - wcf - batch-file - generics - npm - ionic-framework - network-programming - unix - recursion - google-app-engine - mongoose - visual-studio-2010 - .net-core - android-fragments - assembly - animation - math - svg - rust - session - intellij-idea - hadoop - join - curl - winapi - django-models - laravel-5 - next.js - url - heroku - http-redirect - tomcat - inheritance - google-cloud-firestore - webpack - gcc - image-processing - swiftui - keras - asp.net-mvc-4 - logging - dom - matrix - pyspark - actionscript-3 - button - post - optimization - firebase-realtime-database - jquery-ui - cocoa - xpath - iis - web - d3.js - javafx - firefox - xslt - internet-explorer - caching - select - asp.net-mvc-3 - opengl - events - asp.net-web-api - plot - dplyr - encryption - magento - stored-procedures - search - amazon-ec2 - ruby-on-rails-4 - memory - multidimensional-array - canvas - audio - random - jsf - vector - redux - cookies - input - facebook-graph-api - flash - indexing - xamarin.forms - arraylist - ipad - cocoa-touch - data-structures - video - model-view-controller - azure-devops - serialization - apache-kafka - jdbc - razor - awk - woocommerce - routes - mod-rewrite - servlets - excel-formula - beautifulsoup - filter - iframe - docker-compose - design-patterns - aws-lambda - text - visual-c++ - django-rest-framework - cakephp - mobile - android-intent - struct - react-hooks - methods - groovy - mvvm - lambda - ssh - time - checkbox - ecmascript-6 - grails - google-chrome-extension - installation - cmake - sharepoint - shiny - spring-security - jakarta-ee - plsql - android-recyclerview - core-data - types - sed - meteor - android-activity - bootstrap-4 - activerecord - websocket - replace - graph - group-by - scikit-learn - vim - file-upload - boost - junit - memory-management - sass - async-await - import - deep-learning - error-handling - eloquent - dynamic - dependency-injection - soap - silverlight - layout - apache-spark-sql - charts - deployment - browser - gridview - svn - while-loop - google-bigquery - vuejs2 - highcharts - dll - ffmpeg - view - foreach - makefile - plugins - redis - c\#-4.0 - reporting-services - jupyter-notebook - unicode - merge - reflection - https - server - google-maps-api-3 - twitter - oauth-2.0 - extjs - terminal - axios - pip - split - cmd - pytorch - encoding - django-views - collections - database-design - hash - netbeans - automation - data-binding - ember.js - build - tcp - pdo - sqlalchemy - apache-flex - entity-framework-core - concurrency - command-line - spring-data-jpa - printing - react-redux - java-8 - lua - html-table - neo4j - ansible - service - jestjs - mysqli - enums - parameters - flexbox - material-ui - module - promise - visual-studio-2012 - outlook - web-applications - uwp - firebase-authentication - webview - jquery-mobile - utf-8 - datatable - python-requests - parallel-processing - colors - drop-down-menu - scipy - scroll - tfs - hive - count - syntax - ms-word - twitter-bootstrap-3 - ssis - fonts - rxjs - constructor - file-io - google-analytics - paypal - three.js - powerbi - graphql - cassandra - discord - graphics - compiler-errors - gwt - react-router - socket.io - backbone.js - solr - memory-leaks - url-rewriting - datatables - nlp - terraform - oauth - datagridview - drupal - zend-framework - oracle11g - knockout.js - triggers - interface - neural-network - django-forms - casting - angular-material - jmeter - linked-list - google-api - path - timer - arduino - django-templates - orm - windows-phone-7 - proxy - directory - parse-platform - visual-studio-2015 - cron - conditional-statements - push-notification - functional-programming - primefaces - pagination - model - jar - xamarin.android - hyperlink - uiview - vbscript - visual-studio-2013 - google-cloud-functions - azure-active-directory - gitlab - jwt - download - swift3 - sql-server-2005 - process - rspec - configuration - pygame - properties - callback - combobox - windows-phone-8 - linux-kernel - safari - scrapy - emacs - permissions - x86 - clojure - scripting - raspberry-pi - scope - io - azure-functions - compilation - expo - responsive-design - mongodb-query - nhibernate - angularjs-directive - reference - bluetooth - request - binding - architecture - dns - playframework - 3d - pyqt - version-control - discord.js - doctrine-orm - package - f\# - rubygems - get - sql-server-2012 - autocomplete - tree - openssl - datepicker - kendo-ui - jackson - yii - controller - grep - nested - xamarin.ios - static - null - transactions - statistics - datagrid - active-directory - dockerfile - uiviewcontroller - webforms - sas - discord.py - computer-vision - phpmyadmin - notifications - duplicates - pycharm - mocking - youtube - yaml - nullpointerexception - menu - blazor - sum - plotly - bitmap - visual-studio-2008 - asp.net-mvc-5 - floating-point - css-selectors - yii2 - stl - android-listview - electron - jsf-2 - time-series - cryptography - ant - character-encoding - hashmap - stream - msbuild - asp.net-core-mvc - sdk - google-drive-api - jboss - selenium-chromedriver - joomla - devise - navigation - cuda - cors - frontend - anaconda - background - multiprocessing - binary - pyqt5 - camera - iterator - linq-to-sql - mariadb - onclick - android-jetpack-compose - ios7 - microsoft-graph-api - android-asynctask - rabbitmq - tabs - amazon-dynamodb - environment-variables - laravel-4 - uicollectionview - insert - linker - xsd - coldfusion - console - continuous-integration - upload - textview - ftp - opengl-es - operating-system - macros - mockito - localization - formatting - xml-parsing - json.net - type-conversion - vuejs3 - data.table - kivy - timestamp - integer - calendar - segmentation-fault - android-ndk - prolog - char - drag-and-drop - crash - jasmine - dependencies - automated-tests - geometry - azure-pipelines - fortran - android-gradle-plugin - itext - sprite-kit - mfc - header - attributes - nosql - firebase-cloud-messaging - format - nuxt.js - odoo - db2 - jquery-plugins - event-handling - julia - jenkins-pipeline - leaflet - annotations - flutter-layout - nestjs - keyboard - postman - arm - textbox - stripe-payments - visual-studio-2017 - gulp - libgdx - uikit - synchronization - timezone - azure-web-app-service - dom-events - wso2 - xampp - google-sheets-formula - crystal-reports - namespaces - aggregation-framework - android-emulator - uiscrollview - swagger - jvm - sequelize.js - com - chart.js - snowflake-cloud-data-platform - subprocess - geolocation - webdriver - html5-canvas - garbage-collection - sql-update - centos - dialog - concatenation - numbers - widget - qml - tuples - set - java-stream - mapreduce - ionic2 - smtp - windows-10 - android-edittext - rotation - nuget - spring-data - modal-dialog - radio-button - doctrine - http-headers - grid - sonarqube - lucene - xmlhttprequest - listbox - initialization - switch-statement - internationalization - components - boolean - apache-camel - google-play - gdb - serial-port - ios5 - ldap - return - youtube-api - pivot - eclipse-plugin - latex - frameworks - tags - containers - c++17 - subquery - github-actions - embedded - dataset - asp-classic - foreign-keys - label - uinavigationcontroller - delegates - copy - google-cloud-storage - struts2 - migration - protractor - base64 - uibutton - find - queue - sql-server-2008-r2 - arguments - composer-php - append - jaxb - stack - tailwind-css - zip - cucumber - autolayout - ide - entity-framework-6 - iteration - popup - r-markdown - windows-7 - vb6 - airflow - g++ - clang - ssl-certificate - hover - jqgrid - range - gmail Next You’ll be prompted to create an account to view your personalized homepage. 1. 1. [Home](https://stackoverflow.com/) 2. [Questions](https://stackoverflow.com/questions) 3. [AI Assist](https://stackoverflow.com/ai-assist) 4. [Tags](https://stackoverflow.com/tags) 5. [Challenges](https://stackoverflow.com/beta/challenges) 6. [Chat](https://chat.stackoverflow.com/?tab=explore) 7. [Articles](https://stackoverflow.blog/contributed?utm_medium=referral&utm_source=stackoverflow-community&utm_campaign=so-blog&utm_content=experiment-articles) 8. [Users](https://stackoverflow.com/users) 9. [Companies](https://stackoverflow.com/jobs/companies?so_medium=stackoverflow&so_source=SiteNav) 10. [Collectives]() 11. Communities for your favorite technologies. [Explore all Collectives](https://stackoverflow.com/collectives-all) 2. Stack Internal Stack Overflow for Teams is now called **Stack Internal**. Bring the best of human thought and AI automation together at your work. [Try for free](https://stackoverflowteams.com/teams/create/free/?utm_medium=referral&utm_source=stackoverflow-community&utm_campaign=side-bar&utm_content=explore-teams) [Learn more](https://stackoverflow.co/internal/?utm_medium=referral&utm_source=stackoverflow-community&utm_campaign=side-bar&utm_content=explore-teams) 3. [Stack Internal]() 4. Bring the best of human thought and AI automation together at your work. [Learn more](https://stackoverflow.co/internal/?utm_medium=referral&utm_source=stackoverflow-community&utm_campaign=side-bar&utm_content=explore-teams-compact) ##### Collectives™ on Stack Overflow Find centralized, trusted content and collaborate around the technologies you use most. [Learn more about Collectives](https://stackoverflow.com/collectives) **Stack Internal** Knowledge at work Bring the best of human thought and AI automation together at your work. [Explore Stack Internal](https://stackoverflow.co/internal/?utm_medium=referral&utm_source=stackoverflow-community&utm_campaign=side-bar&utm_content=explore-teams-compact-popover) # [Shell script "for" loop syntax](https://stackoverflow.com/questions/1445452/shell-script-for-loop-syntax) [Ask Question](https://stackoverflow.com/questions/ask) Asked 16 years, 4 months ago Modified [1 month ago](https://stackoverflow.com/questions/1445452/shell-script-for-loop-syntax?lastactivity "2025-12-15 00:47:30Z") Viewed 1.2m times This question shows research effort; it is useful and clear 279 Save this question. Show activity on this post. I have gotten the following to work: ``` Copy ``` It produces a bunch of lines of `output: 2`, `output: 3`, so on. However, trying to run the following: ``` Copy ``` produces the following: ``` Copyoutput: {2..10} ``` How can I get the compiler to realize it should treat \$max as the other end of the array, and not part of a string? - [unix](https://stackoverflow.com/questions/tagged/unix "show questions tagged 'unix'") - [syntax](https://stackoverflow.com/questions/tagged/syntax "show questions tagged 'syntax'") - [shell](https://stackoverflow.com/questions/tagged/shell "show questions tagged 'shell'") [Share](https://stackoverflow.com/q/1445452 "Short permalink to this question") Share a link to this question Copy link [CC BY-SA 3.0](https://creativecommons.org/licenses/by-sa/3.0/ "The current license for this post: CC BY-SA 3.0") [Improve this question](https://stackoverflow.com/posts/1445452/edit) Follow Follow this question to receive notifications [edited Jan 19, 2018 at 22:08](https://stackoverflow.com/posts/1445452/revisions "show all edits to this post") [![Peter Mortensen's user avatar](https://i.sstatic.net/RIZKi.png?s=64)](https://stackoverflow.com/users/63550/peter-mortensen) [Peter Mortensen](https://stackoverflow.com/users/63550/peter-mortensen) 31\.2k2222 gold badges110110 silver badges134134 bronze badges asked Sep 18, 2009 at 15:57 [![eykanal's user avatar](https://www.gravatar.com/avatar/8d6be7c07677acaa346e903a497d0acc?s=64&d=identicon&r=PG)](https://stackoverflow.com/users/168775/eykanal) [eykanal](https://stackoverflow.com/users/168775/eykanal) 27\.3k1919 gold badges8686 silver badges114114 bronze badges 6 - 2 what system and shell are you using? What kind of goofy system has sh or bash, but doesn't have seq, a coreutil? whatsisname – [whatsisname](https://stackoverflow.com/users/8159/whatsisname "6,228 reputation") 2009-09-18 16:08:19 +00:00 [Commented Sep 18, 2009 at 16:08](https://stackoverflow.com/questions/1445452/shell-script-for-loop-syntax#comment1291436_1445452) - 13 FreeBSD doesn't. Nietzche-jou – [Nietzche-jou](https://stackoverflow.com/users/39892/nietzche-jou "14,788 reputation") 2009-09-18 16:12:09 +00:00 [Commented Sep 18, 2009 at 16:12](https://stackoverflow.com/questions/1445452/shell-script-for-loop-syntax#comment1291459_1445452) - Small style nit: I usually see the `do` and `then` keywords on the same line as `for` and `if`, respectively. E.g., `for i in {2..10}; do` a paid nerd – [a paid nerd](https://stackoverflow.com/users/102704/a-paid-nerd "31,707 reputation") 2009-09-18 16:33:13 +00:00 [Commented Sep 18, 2009 at 16:33](https://stackoverflow.com/questions/1445452/shell-script-for-loop-syntax#comment1291589_1445452) - possible duplicate of [Is it possible to use a variable in for syntax in bash?](http://stackoverflow.com/questions/17787681/is-it-possible-to-use-a-variable-in-for-syntax-in-bash) Barmar – [Barmar](https://stackoverflow.com/users/1491895/barmar "789,497 reputation") 2013-08-01 15:00:53 +00:00 [Commented Aug 1, 2013 at 15:00](https://stackoverflow.com/questions/1445452/shell-script-for-loop-syntax#comment26315779_1445452) - 1 FreeBSD, at least 10, does have /usr/bin/seq. jrm – [jrm](https://stackoverflow.com/users/1050694/jrm "727 reputation") 2015-05-10 18:46:48 +00:00 [Commented May 10, 2015 at 18:46](https://stackoverflow.com/questions/1445452/shell-script-for-loop-syntax#comment48417686_1445452) \| [Show **1** more comment](https://stackoverflow.com/questions/1445452/shell-script-for-loop-syntax "Expand to show all comments on this post") ## 12 Answers 12 Sorted by: [Reset to default](https://stackoverflow.com/questions/1445452/shell-script-for-loop-syntax?answertab=scoredesc#tab-top) This answer is useful 327 Save this answer. Show activity on this post. Brace expansion, *{x..y}* is performed before other expansions, so you cannot use that for variable length sequences. Instead, use the `seq 2 $max` method as [user *mob* stated](https://stackoverflow.com/questions/1445452/shell-script-for-loop-syntax/1445471#1445471). So, for your example it would be: ``` Copy ``` [Share](https://stackoverflow.com/a/1445507 "Short permalink to this answer") Share a link to this answer Copy link [CC BY-SA 3.0](https://creativecommons.org/licenses/by-sa/3.0/ "The current license for this post: CC BY-SA 3.0") [Improve this answer](https://stackoverflow.com/posts/1445507/edit) Follow Follow this answer to receive notifications [edited Jan 19, 2018 at 22:38](https://stackoverflow.com/posts/1445507/revisions "show all edits to this post") [![Peter Mortensen's user avatar](https://i.sstatic.net/RIZKi.png?s=64)](https://stackoverflow.com/users/63550/peter-mortensen) [Peter Mortensen](https://stackoverflow.com/users/63550/peter-mortensen) 31\.2k2222 gold badges110110 silver badges134134 bronze badges answered Sep 18, 2009 at 16:06 [![whatsisname's user avatar](https://www.gravatar.com/avatar/492597c26215338627bfc33f074a34aa?s=64&d=identicon&r=PG)](https://stackoverflow.com/users/8159/whatsisname) [whatsisname](https://stackoverflow.com/users/8159/whatsisname) 6,22822 gold badges2424 silver badges2828 bronze badges Sign up to request clarification or add additional context in comments. ## 3 Comments Add a comment [![](https://i.sstatic.net/eT4n5.jpg?s=64)](https://stackoverflow.com/users/1600108/miller) miller [miller](https://stackoverflow.com/users/1600108/miller) [Over a year ago](https://stackoverflow.com/questions/1445452/shell-script-for-loop-syntax#comment64518405_1445507) There is no good reason to use an external command such as `seq` to count and increment numbers in the for loop, hence it is recommend that you avoid using `seq`. This command is left for compatibility with old bash. The built-in commands are fast enough. `for (( EXP1; EXP2; EXP3 )) ...` 2016-07-25T09:27:01.267Z+00:00 0 Reply - Copy link [![](https://www.gravatar.com/avatar/be1e7ef6ee96f64e1b60d947df30bac0?s=48&d=identicon&r=PG)](https://stackoverflow.com/users/167897/wernight) Wernight [Wernight](https://stackoverflow.com/users/167897/wernight) [Over a year ago](https://stackoverflow.com/questions/1445452/shell-script-for-loop-syntax#comment66894225_1445507) @miller the `for (( ... ))` syntax isn't POSIX and that means for example that it won't work out of the box on things like Alpine Linux. `seq` does. 2016-09-30T22:21:49.397Z+00:00 18 Reply - Copy link [![](https://i.sstatic.net/UQN4l.jpg?s=64)](https://stackoverflow.com/users/4451732/franklin-yu) Franklin Yu [Franklin Yu](https://stackoverflow.com/users/4451732/franklin-yu) [Over a year ago](https://stackoverflow.com/questions/1445452/shell-script-for-loop-syntax#comment97002463_1445507) @Wernight Not just Alpine Linux; `#!/bin/sh` is Dash in Debian/Ubuntu. 2019-03-12T20:50:46.243Z+00:00 1 Reply - Copy link Add a comment This answer is useful 102 Save this answer. Show activity on this post. Try the arithmetic-expression version of `for`: ``` Copy ``` This is available in most versions of bash, and should be Bourne shell (sh) compatible also. [Share](https://stackoverflow.com/a/1445564 "Short permalink to this answer") Share a link to this answer Copy link [CC BY-SA 3.0](https://creativecommons.org/licenses/by-sa/3.0/ "The current license for this post: CC BY-SA 3.0") [Improve this answer](https://stackoverflow.com/posts/1445564/edit) Follow Follow this answer to receive notifications [edited Jul 12, 2015 at 4:03](https://stackoverflow.com/posts/1445564/revisions "show all edits to this post") answered Sep 18, 2009 at 16:18 [![system PAUSE's user avatar](https://www.gravatar.com/avatar/f979d6d83d40b10d9ad43bdfc51cb1de?s=64&d=identicon&r=PG)](https://stackoverflow.com/users/52963/system-pause) [system PAUSE](https://stackoverflow.com/users/52963/system-pause) 38\.9k2121 gold badges6565 silver badges5959 bronze badges ## 2 Comments Add a comment [![](https://www.gravatar.com/avatar/f979d6d83d40b10d9ad43bdfc51cb1de?s=48&d=identicon&r=PG)](https://stackoverflow.com/users/52963/system-pause) system PAUSE [system PAUSE](https://stackoverflow.com/users/52963/system-pause) [Over a year ago](https://stackoverflow.com/questions/1445452/shell-script-for-loop-syntax#comment12601002_1445564) @Flow: Hm, I just tried it on a couple of systems (Linux and BSD based) with \#!/bin/sh and it worked fine. Invoked under bash and specifically under /bin/sh, still worked. Maybe the version of sh matters? Are you on some old Unix? 2012-03-26T21:29:02.433Z+00:00 1 Reply - Copy link [![](https://www.gravatar.com/avatar/fa05233b2357f8d11c22ef4cfc7bb85c?s=48&d=identicon&r=PG)](https://stackoverflow.com/users/1126841/chepner) chepner [chepner](https://stackoverflow.com/users/1126841/chepner) [Over a year ago](https://stackoverflow.com/questions/1445452/shell-script-for-loop-syntax#comment27676821_1445564) Very few systems have a dedicated `sh`, instead making it a link to other another shell. Ideally, such a shell invoked as `sh` would *only* support those features in the POSIX standard, but by default let some of their extra features through. The C-style for-loop is not a POSIX feature, but may be in `sh` mode by the actual shell. 2013-09-12T19:41:22.943Z+00:00 8 Reply - Copy link This answer is useful 40 Save this answer. Show activity on this post. Step the loop manually: ``` i=0 max=10 while [ $i -lt $max ] do echo "output: $i" true $(( i++ )) done ``` If you don’t have to be totally POSIX, you can use the arithmetic *for* loop: ``` max=10 for (( i=0; i < max; i++ )); do echo "output: $i"; done ``` Or use *jot*(1) on BSD systems: ``` for i in $( jot 0 10 ); do echo "output: $i"; done ``` [Share](https://stackoverflow.com/a/1445562 "Short permalink to this answer") Share a link to this answer Copy link [CC BY-SA 2.5](https://creativecommons.org/licenses/by-sa/2.5/ "The current license for this post: CC BY-SA 2.5") [Improve this answer](https://stackoverflow.com/posts/1445562/edit) Follow Follow this answer to receive notifications [edited Sep 18, 2009 at 16:32](https://stackoverflow.com/posts/1445562/revisions "show all edits to this post") answered Sep 18, 2009 at 16:17 [![Nietzche-jou's user avatar](https://www.gravatar.com/avatar/2533c49c0597021c8ef573bf08be8528?s=64&d=identicon&r=PG)](https://stackoverflow.com/users/39892/nietzche-jou) [Nietzche-jou](https://stackoverflow.com/users/39892/nietzche-jou) 14\.8k44 gold badges3737 silver badges4646 bronze badges ## 3 Comments Add a comment [![](https://www.gravatar.com/avatar/5e5b6587ba56fcaae86fe5db95248e98?s=48&d=identicon&r=PG)](https://stackoverflow.com/users/194894/flow) Flow [Flow](https://stackoverflow.com/users/194894/flow) [Over a year ago](https://stackoverflow.com/questions/1445452/shell-script-for-loop-syntax#comment12516457_1445562) `true $(( i++ ))` doesn't work in all cases, so most portable would be `true $((i=i+1))`. 2012-03-22T14:50:01.037Z+00:00 5 Reply - Copy link [![](https://i.sstatic.net/3jvVV.jpg?s=64)](https://stackoverflow.com/users/975199/logan) logan [logan](https://stackoverflow.com/users/975199/logan) [Over a year ago](https://stackoverflow.com/questions/1445452/shell-script-for-loop-syntax#comment32769199_1445562) semi colon should not come in "for (( i=0; i \< max; i++ ));" 2014-02-10T13:02:15.42Z+00:00 0 Reply - Copy link [![](https://lh3.googleusercontent.com/a-/AOh14Gh77XbK_ZXsqVwrM6a6K6g2DNN4_XD_K1wOopWCVA=k-s48)](https://stackoverflow.com/users/14672114/rare-kpop-manifesto) RARE Kpop Manifesto [RARE Kpop Manifesto](https://stackoverflow.com/users/14672114/rare-kpop-manifesto) [Over a year ago](https://stackoverflow.com/questions/1445452/shell-script-for-loop-syntax#comment138451772_1445562) `jot 0 10` might be an infinite loop starting at 10. To get the same effect as `max=10; for (( i=0; i < max; i++ )); do echo "output: $i"; done` I think the syntax should be `jot -w 'output: %d' 10 0` (directly print out sans loop) or just `for i in $( jot 10 0 ); do …..` for the looped variant 2024-05-26T01:20:19.787Z+00:00 0 Reply - Copy link Add a comment This answer is useful 22 Save this answer. Show activity on this post. If the `seq` command available on your system: ``` Copy ``` If not, then use poor man's `seq` with `perl`: ``` Copy ``` Watch those quote marks. [Share](https://stackoverflow.com/a/1445471 "Short permalink to this answer") Share a link to this answer Copy link [CC BY-SA 3.0](https://creativecommons.org/licenses/by-sa/3.0/ "The current license for this post: CC BY-SA 3.0") [Improve this answer](https://stackoverflow.com/posts/1445471/edit) Follow Follow this answer to receive notifications [edited Jan 19, 2018 at 23:10](https://stackoverflow.com/posts/1445471/revisions "show all edits to this post") answered Sep 18, 2009 at 16:01 [![mob's user avatar](https://www.gravatar.com/avatar/f14e7610fca39322ebbf23530def15f8?s=64&d=identicon&r=PG)](https://stackoverflow.com/users/168657/mob) [mob](https://stackoverflow.com/users/168657/mob) 119k1818 gold badges159159 silver badges291291 bronze badges ## Comments Add a comment This answer is useful 19 Save this answer. Show activity on this post. We can iterate loop like as C programming. ``` Copy ``` [Share](https://stackoverflow.com/a/51455910 "Short permalink to this answer") Share a link to this answer Copy link [CC BY-SA 4.0](https://creativecommons.org/licenses/by-sa/4.0/ "The current license for this post: CC BY-SA 4.0") [Improve this answer](https://stackoverflow.com/posts/51455910/edit) Follow Follow this answer to receive notifications answered Jul 21, 2018 at 12:07 [![rashedcs's user avatar](https://i.sstatic.net/adedu.jpg?s=64)](https://stackoverflow.com/users/6714430/rashedcs) [rashedcs](https://stackoverflow.com/users/6714430/rashedcs) 3,76322 gold badges4343 silver badges4444 bronze badges ## Comments Add a comment This answer is useful 10 Save this answer. Show activity on this post. There's more than one way to do it. ``` Copy ``` [Share](https://stackoverflow.com/a/1445577 "Short permalink to this answer") Share a link to this answer Copy link [CC BY-SA 2.5](https://creativecommons.org/licenses/by-sa/2.5/ "The current license for this post: CC BY-SA 2.5") [Improve this answer](https://stackoverflow.com/posts/1445577/edit) Follow Follow this answer to receive notifications answered Sep 18, 2009 at 16:20 [![ephemient's user avatar](https://www.gravatar.com/avatar/09b9758a4a83cc25547eb93891f19df7?s=64&d=identicon&r=PG)](https://stackoverflow.com/users/20713/ephemient) [ephemient](https://stackoverflow.com/users/20713/ephemient) 207k4040 gold badges293293 silver badges404404 bronze badges ## 2 Comments Add a comment [![](https://www.gravatar.com/avatar/fa05233b2357f8d11c22ef4cfc7bb85c?s=48&d=identicon&r=PG)](https://stackoverflow.com/users/1126841/chepner) chepner [chepner](https://stackoverflow.com/users/1126841/chepner) [Over a year ago](https://stackoverflow.com/questions/1445452/shell-script-for-loop-syntax#comment27676920_1445577) A security risk, since `eval` will evaluate *anything* you set `max` to. Consider `max="2}; echo ha; #"`, then replace `echo ha` with something more destructive. 2013-09-12T19:45:27.713Z+00:00 7 Reply - Copy link [![](https://www.gravatar.com/avatar/c0c1d6fa6f329573985fa292569adc24?s=48&d=identicon&r=PG)](https://stackoverflow.com/users/218830/conrad-meyer) Conrad Meyer [Conrad Meyer](https://stackoverflow.com/users/218830/conrad-meyer) [Over a year ago](https://stackoverflow.com/questions/1445452/shell-script-for-loop-syntax#comment35468335_1445577) (S)he set max to 10. No risk. 2014-04-21T01:13:16.847Z+00:00 7 Reply - Copy link This answer is useful 5 Save this answer. Show activity on this post. This is a way: Bash: ``` Copy ``` The above Bash way will work for `ksh` and `zsh` too, when `bash -c` is replaced with `ksh -c` or `zsh -c` respectively. Note: `for i in {2..${max}}; do echo $i; done` works in `zsh` and `ksh`. [Share](https://stackoverflow.com/a/31367780 "Short permalink to this answer") Share a link to this answer Copy link [CC BY-SA 3.0](https://creativecommons.org/licenses/by-sa/3.0/ "The current license for this post: CC BY-SA 3.0") [Improve this answer](https://stackoverflow.com/posts/31367780/edit) Follow Follow this answer to receive notifications [edited Jul 12, 2015 at 14:11](https://stackoverflow.com/posts/31367780/revisions "show all edits to this post") answered Jul 12, 2015 at 12:30 [![Jahid's user avatar](https://i.sstatic.net/XvLrx.jpg?s=64)](https://stackoverflow.com/users/3744681/jahid) [Jahid](https://stackoverflow.com/users/3744681/jahid) 22\.6k1010 gold badges9797 silver badges114114 bronze badges ## Comments Add a comment This answer is useful 3 Save this answer. Show activity on this post. Well, as I didn't have the `seq` command installed on my system (Mac OS X v10.6.1 (Snow Leopard)), I ended up using a `while` loop instead: ``` Copy ``` \**Shrugs*\* Whatever works. [Share](https://stackoverflow.com/a/1445552 "Short permalink to this answer") Share a link to this answer Copy link [CC BY-SA 3.0](https://creativecommons.org/licenses/by-sa/3.0/ "The current license for this post: CC BY-SA 3.0") [Improve this answer](https://stackoverflow.com/posts/1445552/edit) Follow Follow this answer to receive notifications [edited Jan 19, 2018 at 22:22](https://stackoverflow.com/posts/1445552/revisions "show all edits to this post") [![Peter Mortensen's user avatar](https://i.sstatic.net/RIZKi.png?s=64)](https://stackoverflow.com/users/63550/peter-mortensen) [Peter Mortensen](https://stackoverflow.com/users/63550/peter-mortensen) 31\.2k2222 gold badges110110 silver badges134134 bronze badges answered Sep 18, 2009 at 16:16 [![eykanal's user avatar](https://www.gravatar.com/avatar/8d6be7c07677acaa346e903a497d0acc?s=64&d=identicon&r=PG)](https://stackoverflow.com/users/168775/eykanal) [eykanal](https://stackoverflow.com/users/168775/eykanal) 27\.3k1919 gold badges8686 silver badges114114 bronze badges ## 1 Comment Add a comment [![](https://www.gravatar.com/avatar/f979d6d83d40b10d9ad43bdfc51cb1de?s=48&d=identicon&r=PG)](https://stackoverflow.com/users/52963/system-pause) system PAUSE [system PAUSE](https://stackoverflow.com/users/52963/system-pause) [Over a year ago](https://stackoverflow.com/questions/1445452/shell-script-for-loop-syntax#comment1291527_1445552) seq is relatively new. I only found out about it a few months ago. But you *can* use a 'for' loop!! The disadvantage of a 'while' is that you have to remember to increment the counter somewhere inside the loop, or else loop downwards. 2009-09-18T16:23:03.563Z+00:00 2 Reply - Copy link This answer is useful 2 Save this answer. Show activity on this post. These all do `{1..8}` and should all be POSIX. They also will not break if you put a conditional `continue` in the loop. The canonical way: ``` Copy ``` Another way: ``` Copy ``` and another: ``` Copy ``` [Share](https://stackoverflow.com/a/34374215 "Short permalink to this answer") Share a link to this answer Copy link [CC BY-SA 3.0](https://creativecommons.org/licenses/by-sa/3.0/ "The current license for this post: CC BY-SA 3.0") [Improve this answer](https://stackoverflow.com/posts/34374215/edit) Follow Follow this answer to receive notifications [edited Dec 19, 2015 at 22:32](https://stackoverflow.com/posts/34374215/revisions "show all edits to this post") answered Dec 19, 2015 at 18:51 user4427511 ## 1 Comment Add a comment [![](https://www.gravatar.com/avatar/37c028b8688476f507f012b17f70006d?s=48&d=identicon&r=PG)](https://stackoverflow.com/users/11338291/kkocdko) kkocdko [kkocdko](https://stackoverflow.com/users/11338291/kkocdko) [Over a year ago](https://stackoverflow.com/questions/1445452/shell-script-for-loop-syntax#comment132808667_34374215) Wonderful, I'm looking for a solution for busybox's ash, and your answer is perfect 2023-01-27T19:57:03.243Z+00:00 0 Reply - Copy link This answer is useful 2 Save this answer. Show activity on this post. Here it worked on Mac OS X. It includes the example of a BSD date, how to increment and decrement the date also: ``` Copy ``` [Share](https://stackoverflow.com/a/20416848 "Short permalink to this answer") Share a link to this answer Copy link [CC BY-SA 3.0](https://creativecommons.org/licenses/by-sa/3.0/ "The current license for this post: CC BY-SA 3.0") [Improve this answer](https://stackoverflow.com/posts/20416848/edit) Follow Follow this answer to receive notifications [edited Jan 19, 2018 at 22:17](https://stackoverflow.com/posts/20416848/revisions "show all edits to this post") [![Peter Mortensen's user avatar](https://i.sstatic.net/RIZKi.png?s=64)](https://stackoverflow.com/users/63550/peter-mortensen) [Peter Mortensen](https://stackoverflow.com/users/63550/peter-mortensen) 31\.2k2222 gold badges110110 silver badges134134 bronze badges answered Dec 6, 2013 at 5:42 [![minhas23's user avatar](https://i.sstatic.net/Tz7kk.jpg?s=64)](https://stackoverflow.com/users/2458916/minhas23) [minhas23](https://stackoverflow.com/users/2458916/minhas23) 9,76166 gold badges6060 silver badges4040 bronze badges ## Comments Add a comment This answer is useful 1 Save this answer. Show activity on this post. Use: ``` Copy ``` You need the explicit 'eval' call to reevaluate the {} after variable substitution. [Share](https://stackoverflow.com/a/1445612 "Short permalink to this answer") Share a link to this answer Copy link [CC BY-SA 3.0](https://creativecommons.org/licenses/by-sa/3.0/ "The current license for this post: CC BY-SA 3.0") [Improve this answer](https://stackoverflow.com/posts/1445612/edit) Follow Follow this answer to receive notifications [edited Jan 19, 2018 at 22:15](https://stackoverflow.com/posts/1445612/revisions "show all edits to this post") [![Peter Mortensen's user avatar](https://i.sstatic.net/RIZKi.png?s=64)](https://stackoverflow.com/users/63550/peter-mortensen) [Peter Mortensen](https://stackoverflow.com/users/63550/peter-mortensen) 31\.2k2222 gold badges110110 silver badges134134 bronze badges answered Sep 18, 2009 at 16:27 [![Chris Dodd's user avatar](https://www.gravatar.com/avatar/0f02eed6d98a2b824e209ecd7bbddb6a?s=64&d=identicon&r=PG)](https://stackoverflow.com/users/29759/chris-dodd) [Chris Dodd](https://stackoverflow.com/users/29759/chris-dodd) 2,9781515 silver badges1010 bronze badges ## Comments Add a comment This answer is useful 0 Save this answer. Show activity on this post. Use `seq 2 10 | xargs printf 'output: %d\n'`, if you only need the output, and the loop may be implicit. [Share](https://stackoverflow.com/a/79847322 "Short permalink to this answer") Share a link to this answer Copy link [CC BY-SA 4.0](https://creativecommons.org/licenses/by-sa/4.0/ "The current license for this post: CC BY-SA 4.0") [Improve this answer](https://stackoverflow.com/posts/79847322/edit) Follow Follow this answer to receive notifications answered Dec 15, 2025 at 0:47 [![Jens Jensen's user avatar](https://www.gravatar.com/avatar/eb0885cbf28359cf375207765028afe5?s=64&d=identicon&r=PG)](https://stackoverflow.com/users/1235724/jens-jensen) [Jens Jensen](https://stackoverflow.com/users/1235724/jens-jensen) 1,12811 gold badge1010 silver badges2222 bronze badges ## Comments Add a comment **[Protected question](https://stackoverflow.com/help/privileges/protect-questions)**. To answer this question, you need to have at least 10 reputation on this site (not counting the [association bonus](https://meta.stackexchange.com/questions/141648/what-is-the-association-bonus-and-how-does-it-work)). The reputation requirement helps protect this question from spam and non-answer activity. Start asking to get answers Find the answer to your question by asking. [Ask question](https://stackoverflow.com/questions/ask) Explore related questions - [unix](https://stackoverflow.com/questions/tagged/unix "show questions tagged 'unix'") - [syntax](https://stackoverflow.com/questions/tagged/syntax "show questions tagged 'syntax'") - [shell](https://stackoverflow.com/questions/tagged/shell "show questions tagged 'shell'") See similar questions with these tags. - The Overflow Blog - [What’s new at Stack Overflow: February 2026](https://stackoverflow.blog/2026/02/02/what-s-new-at-stack-overflow-february-2026/?cb=1) - [Generating text with diffusion (and ROI with LLMs)](https://stackoverflow.blog/2026/02/03/generating-text-with-diffusion-and-roi-with-llms/?cb=1) - Featured on Meta - [Results of the January 2026 Community Asks Sprint: Community Badges](https://meta.stackexchange.com/questions/417043/results-of-the-january-2026-community-asks-sprint-community-badges?cb=1) - [All users on Stack Exchange can now participate in chat](https://meta.stackexchange.com/questions/417109/all-users-on-stack-exchange-can-now-participate-in-chat?cb=1) - [Policy: Generative AI (e.g., ChatGPT) is banned](https://meta.stackoverflow.com/questions/421831/policy-generative-ai-e-g-chatgpt-is-banned?cb=1) - [Stack Overflow now uses machine learning to flag spam automatically](https://meta.stackoverflow.com/questions/437959/stack-overflow-now-uses-machine-learning-to-flag-spam-automatically?cb=1) - [No, I do not believe this is the end](https://meta.stackoverflow.com/questions/438009/no-i-do-not-believe-this-is-the-end?cb=1) [Visit chat](https://chat.stackoverflow.com/) Community activity Last 1 hr - Users online activity 16871 users online - 24 questions - 22 answers - 60 comments - 292 upvotes Popular tags [javascript](https://stackoverflow.com/questions/tagged/javascript)[c\#](https://stackoverflow.com/questions/tagged/c)[java](https://stackoverflow.com/questions/tagged/java)[python](https://stackoverflow.com/questions/tagged/python)[.net](https://stackoverflow.com/questions/tagged/.net)[c++](https://stackoverflow.com/questions/tagged/c++) Popular unanswered question [Docker containers consuming all the inodes of linux machine](https://stackoverflow.com/questions/75029900) [linux](https://stackoverflow.com/questions/tagged/linux)[docker](https://stackoverflow.com/questions/tagged/docker)[inode](https://stackoverflow.com/questions/tagged/inode) [![User avatar](https://www.gravatar.com/avatar/338e5af11dd6a4eaac21549c5b6a3a64?s=256&d=identicon&r=PG&f=y&so-version=2)](https://stackoverflow.com/users/1463746) [Nagri](https://stackoverflow.com/users/1463746) - 3\.2k 1,124 days ago #### Linked [0](https://stackoverflow.com/questions/72166016/run-a-bash-for-loop-zero-times?lq=1 "Question score (upvotes - downvotes)") [Run a bash for loop zero times](https://stackoverflow.com/questions/72166016/run-a-bash-for-loop-zero-times?noredirect=1&lq=1) [0](https://stackoverflow.com/questions/63959299/nohup-does-not-work-with-bash-for-loop-properly?lq=1 "Question score (upvotes - downvotes)") [Nohup does not work with bash for loop properly](https://stackoverflow.com/questions/63959299/nohup-does-not-work-with-bash-for-loop-properly?noredirect=1&lq=1) [2](https://stackoverflow.com/questions/17787681/is-it-possible-to-use-a-variable-in-for-syntax-in-bash?lq=1 "Question score (upvotes - downvotes)") [Is it possible to use a variable in for syntax in bash?](https://stackoverflow.com/questions/17787681/is-it-possible-to-use-a-variable-in-for-syntax-in-bash?noredirect=1&lq=1) [1](https://stackoverflow.com/questions/17872159/for-loop-in-unix?lq=1 "Question score (upvotes - downvotes)") [For loop in Unix](https://stackoverflow.com/questions/17872159/for-loop-in-unix?noredirect=1&lq=1) [1](https://stackoverflow.com/questions/28623266/evaluate-expression-inside-bash-for-loop?lq=1 "Question score (upvotes - downvotes)") [Evaluate expression inside bash for loop](https://stackoverflow.com/questions/28623266/evaluate-expression-inside-bash-for-loop?noredirect=1&lq=1) [1](https://stackoverflow.com/questions/69869270/loop-does-not-work-normally-in-git-action?lq=1 "Question score (upvotes - downvotes)") [Loop does not work normally in git action](https://stackoverflow.com/questions/69869270/loop-does-not-work-normally-in-git-action?noredirect=1&lq=1) [0](https://stackoverflow.com/questions/71261892/automate-swipe-action-on-android-studio-emulator?lq=1 "Question score (upvotes - downvotes)") [automate swipe action on android studio emulator](https://stackoverflow.com/questions/71261892/automate-swipe-action-on-android-studio-emulator?noredirect=1&lq=1) #### Related [0](https://stackoverflow.com/questions/2999648/shell-scripting-for-loop-syntax-error?rq=3 "Question score (upvotes - downvotes)") [Shell Scripting For loop Syntax Error](https://stackoverflow.com/questions/2999648/shell-scripting-for-loop-syntax-error?rq=3) [12](https://stackoverflow.com/questions/6854118/bash-for-loop-syntax?rq=3 "Question score (upvotes - downvotes)") [Bash 'for' loop syntax?](https://stackoverflow.com/questions/6854118/bash-for-loop-syntax?rq=3) [0](https://stackoverflow.com/questions/9110093/simple-for-loop-in-shell-script?rq=3 "Question score (upvotes - downvotes)") [Simple for-loop in shell script](https://stackoverflow.com/questions/9110093/simple-for-loop-in-shell-script?rq=3) [1](https://stackoverflow.com/questions/17872159/for-loop-in-unix?rq=3 "Question score (upvotes - downvotes)") [For loop in Unix](https://stackoverflow.com/questions/17872159/for-loop-in-unix?rq=3) [1](https://stackoverflow.com/questions/23528592/shell-for-loop-syntax-error?rq=3 "Question score (upvotes - downvotes)") [Shell for loop syntax error](https://stackoverflow.com/questions/23528592/shell-for-loop-syntax-error?rq=3) [1](https://stackoverflow.com/questions/27628016/for-loop-in-unix-shell-scripting?rq=3 "Question score (upvotes - downvotes)") [For loop in unix shell scripting](https://stackoverflow.com/questions/27628016/for-loop-in-unix-shell-scripting?rq=3) [1](https://stackoverflow.com/questions/31089036/for-loop-in-shell-script?rq=3 "Question score (upvotes - downvotes)") [For loop in shell script](https://stackoverflow.com/questions/31089036/for-loop-in-shell-script?rq=3) [0](https://stackoverflow.com/questions/37394940/bash-for-loop-syntax-explanation?rq=3 "Question score (upvotes - downvotes)") [Bash for loop syntax explanation](https://stackoverflow.com/questions/37394940/bash-for-loop-syntax-explanation?rq=3) [0](https://stackoverflow.com/questions/40600521/for-loop-syntax?rq=3 "Question score (upvotes - downvotes)") [For loop syntax?](https://stackoverflow.com/questions/40600521/for-loop-syntax?rq=3) [1](https://stackoverflow.com/questions/42162615/bash-for-loop-syntax?rq=3 "Question score (upvotes - downvotes)") [Bash for loop syntax](https://stackoverflow.com/questions/42162615/bash-for-loop-syntax?rq=3) #### [Hot Network Questions](https://stackexchange.com/questions?tab=hot) - [Recommended repair method for separated conduit at service entrance? USA](https://diy.stackexchange.com/questions/329001/recommended-repair-method-for-separated-conduit-at-service-entrance-usa) - [Falsifiability of religious language](https://philosophy.stackexchange.com/questions/135809/falsifiability-of-religious-language) - [What does “appoint elders” mean in the New Testament?](https://hermeneutics.stackexchange.com/questions/114460/what-does-appoint-elders-mean-in-the-new-testament) - [Dell laptop battery not charging beyond 60%](https://unix.stackexchange.com/questions/804145/dell-laptop-battery-not-charging-beyond-60) - [Possible typo in Bach Flute Partita in A minor](https://music.stackexchange.com/questions/143167/possible-typo-in-bach-flute-partita-in-a-minor) - [On the distribution of orders of a fixed norm-1 unit modulo inert primes](https://mathoverflow.net/questions/507727/on-the-distribution-of-orders-of-a-fixed-norm-1-unit-modulo-inert-primes) - [What are some references on suggestions to consider targets of our attention as objectively existing things?](https://philosophy.stackexchange.com/questions/135816/what-are-some-references-on-suggestions-to-consider-targets-of-our-attention-as) - [Which “status” should a lecturer choose when registering on arXiv?](https://academia.stackexchange.com/questions/225810/which-status-should-a-lecturer-choose-when-registering-on-arxiv) - [App won't install on one of two "identical" tablets](https://android.stackexchange.com/questions/264944/app-wont-install-on-one-of-two-identical-tablets) - [Is it portable to cast an unsigned value to its signed version to prevent underflow?](https://stackoverflow.com/questions/79880273/is-it-portable-to-cast-an-unsigned-value-to-its-signed-version-to-prevent-underf) - [Do we have an English word to express the action of releasing air and producing the sound "Ha" after drinking?](https://ell.stackexchange.com/questions/373850/do-we-have-an-english-word-to-express-the-action-of-releasing-air-and-producing) - [Breaker vs Wiring vs Outlet - Does this thinking make sense?](https://diy.stackexchange.com/questions/329013/breaker-vs-wiring-vs-outlet-does-this-thinking-make-sense) - [Wrapping a circle on a torus](https://tex.stackexchange.com/questions/759057/wrapping-a-circle-on-a-torus) - [Ship or a building discovered on the moon, an old fashioned railway trolley](https://scifi.stackexchange.com/questions/303097/ship-or-a-building-discovered-on-the-moon-an-old-fashioned-railway-trolley) - [Can I recover data from a live installation medium, or is everything stored in RAM?](https://unix.stackexchange.com/questions/804159/can-i-recover-data-from-a-live-installation-medium-or-is-everything-stored-in-r) - [Is there any way to unlock the purchasable Spider-Man suits now that the store is defunct?](https://gaming.stackexchange.com/questions/417763/is-there-any-way-to-unlock-the-purchasable-spider-man-suits-now-that-the-store-i) - [Why is the Big Rip scenario of the end of Universe only possible if "phantom energy" exists?](https://physics.stackexchange.com/questions/868587/why-is-the-big-rip-scenario-of-the-end-of-universe-only-possible-if-phantom-ene) - [MCP7940N Real Time Clock IC - Treatment of MFP pin when running from VBAT](https://electronics.stackexchange.com/questions/765242/mcp7940n-real-time-clock-ic-treatment-of-mfp-pin-when-running-from-vbat) - [Recovery of Platinum from Sediment After Hydrogen Peroxide Exposure of Platinized Titanium Anodes](https://chemistry.stackexchange.com/questions/194930/recovery-of-platinum-from-sediment-after-hydrogen-peroxide-exposure-of-platinize) - [Why does this LCD PCB have an "island" cut out in it?](https://electronics.stackexchange.com/questions/765171/why-does-this-lcd-pcb-have-an-island-cut-out-in-it) - [Reducing a hand wrist edge loop from 18 to 8 vertices for SubD animation](https://blender.stackexchange.com/questions/344908/reducing-a-hand-wrist-edge-loop-from-18-to-8-vertices-for-subd-animation) - [Unix WC Implementation](https://codereview.stackexchange.com/questions/301155/unix-wc-implementation) - [Word order when combining a name with a noun](https://german.stackexchange.com/questions/82096/word-order-when-combining-a-name-with-a-noun) - [What is it to write something "formally"?](https://philosophy.stackexchange.com/questions/135855/what-is-it-to-write-something-formally) [Question feed](https://stackoverflow.com/feeds/question/1445452 "Feed of this question and its answers") # Subscribe to RSS Question feed To subscribe to this RSS feed, copy and paste this URL into your RSS reader. ![](https://stackoverflow.com/posts/1445452/ivc/1ea6?prg=ff327940-f27f-4775-8577-4b7b868220b0) lang-bash # Why are you flagging this comment? Probable spam. This comment promotes a product, service or website while [failing to disclose the author's affiliation](https://stackoverflow.com/help/promotion). Unfriendly or contains harassment/bigotry/abuse. This comment is unkind, insulting or attacks another person or group. Learn more in our [Abusive behavior policy](https://stackoverflow.com/conduct/abusive-behavior). Not needed. This comment is not relevant to the post. ``` ``` Enter at least 6 characters Something else. A problem not listed above. Try to be as specific as possible. ``` ``` Enter at least 6 characters Flag comment Cancel You have 0 flags left today # ![Illustration of upvote icon after it is clicked](https://stackoverflow.com/Content/Img/modal/img-upvote.png?v=fce73bd9724d) # Hang on, you can't upvote just yet. You'll need to complete a few actions and gain 15 reputation points before being able to upvote. **Upvoting** indicates when questions and answers are useful. [What's reputation and how do I get it?](https://stackoverflow.com/help/whats-reputation) Instead, you can save this post to reference later. Save this post for later Not now ##### [Stack Overflow](https://stackoverflow.com/) - [Questions](https://stackoverflow.com/questions) - [Help](https://stackoverflow.com/help) - [Chat](https://chat.stackoverflow.com/?tab=explore) ##### [Business](https://stackoverflow.co/) - [Stack Internal](https://stackoverflow.co/internal/?utm_medium=referral&utm_source=stackoverflow-community&utm_campaign=footer&utm_content=teams) - [Stack Data Licensing](https://stackoverflow.co/data-licensing/?utm_medium=referral&utm_source=stackoverflow-community&utm_campaign=footer&utm_content=data-licensing) - [Stack Ads](https://stackoverflow.co/advertising/?utm_medium=referral&utm_source=stackoverflow-community&utm_campaign=footer&utm_content=advertising) ##### [Company](https://stackoverflow.co/) - [About](https://stackoverflow.co/) - [Press](https://stackoverflow.co/company/press/) - [Work Here](https://stackoverflow.co/company/work-here/) - [Legal](https://stackoverflow.com/legal) - [Privacy Policy](https://stackoverflow.com/legal/privacy-policy) - [Terms of Service](https://stackoverflow.com/legal/terms-of-service/public) - [Contact Us](https://stackoverflow.com/contact) - Cookie Settings - [Cookie Policy](https://policies.stackoverflow.co/stack-overflow/cookie-policy) ##### [Stack Exchange Network](https://stackexchange.com/) - [Technology](https://stackexchange.com/sites#technology) - [Culture & recreation](https://stackexchange.com/sites#culturerecreation) - [Life & arts](https://stackexchange.com/sites#lifearts) - [Science](https://stackexchange.com/sites#science) - [Professional](https://stackexchange.com/sites#professional) - [Business](https://stackexchange.com/sites#business) - [API](https://api.stackexchange.com/) - [Data](https://data.stackexchange.com/) - [Blog](https://stackoverflow.blog/?blb=1) - [Facebook](https://www.facebook.com/officialstackoverflow/) - [Twitter](https://twitter.com/stackoverflow) - [LinkedIn](https://linkedin.com/company/stack-overflow) - [Instagram](https://www.instagram.com/thestackoverflow) Site design / logo © 2026 Stack Exchange Inc; user contributions licensed under [CC BY-SA](https://stackoverflow.com/help/licensing) . rev 2026.2.2.39324
Readable Markdown
This question shows research effort; it is useful and clear 279 Save this question. Show activity on this post. I have gotten the following to work: ``` for i in {2..10} do echo "output: $i" done ``` It produces a bunch of lines of `output: 2`, `output: 3`, so on. However, trying to run the following: ``` max=10 for i in {2..$max} do echo "$i" done ``` produces the following: ``` output: {2..10} ``` How can I get the compiler to realize it should treat \$max as the other end of the array, and not part of a string? [![Peter Mortensen's user avatar](https://i.sstatic.net/RIZKi.png?s=64)](https://stackoverflow.com/users/63550/peter-mortensen) asked Sep 18, 2009 at 15:57 [![eykanal's user avatar](https://www.gravatar.com/avatar/8d6be7c07677acaa346e903a497d0acc?s=64&d=identicon&r=PG)](https://stackoverflow.com/users/168775/eykanal) 6 This answer is useful 327 Save this answer. Show activity on this post. Brace expansion, *{x..y}* is performed before other expansions, so you cannot use that for variable length sequences. Instead, use the `seq 2 $max` method as [user *mob* stated](https://stackoverflow.com/questions/1445452/shell-script-for-loop-syntax/1445471#1445471). So, for your example it would be: ``` max=10 for i in `seq 2 $max` do echo "$i" done ``` [![Peter Mortensen's user avatar](https://i.sstatic.net/RIZKi.png?s=64)](https://stackoverflow.com/users/63550/peter-mortensen) answered Sep 18, 2009 at 16:06 [![whatsisname's user avatar](https://www.gravatar.com/avatar/492597c26215338627bfc33f074a34aa?s=64&d=identicon&r=PG)](https://stackoverflow.com/users/8159/whatsisname) 3 Comments [![](https://i.sstatic.net/eT4n5.jpg?s=64)](https://stackoverflow.com/users/1600108/miller) There is no good reason to use an external command such as `seq` to count and increment numbers in the for loop, hence it is recommend that you avoid using `seq`. This command is left for compatibility with old bash. The built-in commands are fast enough. `for (( EXP1; EXP2; EXP3 )) ...` 2016-07-25T09:27:01.267Z+00:00 [![](https://www.gravatar.com/avatar/be1e7ef6ee96f64e1b60d947df30bac0?s=48&d=identicon&r=PG)](https://stackoverflow.com/users/167897/wernight) @miller the `for (( ... ))` syntax isn't POSIX and that means for example that it won't work out of the box on things like Alpine Linux. `seq` does. 2016-09-30T22:21:49.397Z+00:00 [![](https://i.sstatic.net/UQN4l.jpg?s=64)](https://stackoverflow.com/users/4451732/franklin-yu) @Wernight Not just Alpine Linux; `#!/bin/sh` is Dash in Debian/Ubuntu. 2019-03-12T20:50:46.243Z+00:00 This answer is useful 102 Save this answer. Show activity on this post. Try the arithmetic-expression version of `for`: ``` max=10 for (( i=2; i <= $max; ++i )) do echo "$i" done ``` This is available in most versions of bash, and should be Bourne shell (sh) compatible also. answered Sep 18, 2009 at 16:18 [![system PAUSE's user avatar](https://www.gravatar.com/avatar/f979d6d83d40b10d9ad43bdfc51cb1de?s=64&d=identicon&r=PG)](https://stackoverflow.com/users/52963/system-pause) [system PAUSE](https://stackoverflow.com/users/52963/system-pause) 38\.9k21 gold badges65 silver badges59 bronze badges 2 Comments [![](https://www.gravatar.com/avatar/f979d6d83d40b10d9ad43bdfc51cb1de?s=48&d=identicon&r=PG)](https://stackoverflow.com/users/52963/system-pause) @Flow: Hm, I just tried it on a couple of systems (Linux and BSD based) with \#!/bin/sh and it worked fine. Invoked under bash and specifically under /bin/sh, still worked. Maybe the version of sh matters? Are you on some old Unix? 2012-03-26T21:29:02.433Z+00:00 [![](https://www.gravatar.com/avatar/fa05233b2357f8d11c22ef4cfc7bb85c?s=48&d=identicon&r=PG)](https://stackoverflow.com/users/1126841/chepner) Very few systems have a dedicated `sh`, instead making it a link to other another shell. Ideally, such a shell invoked as `sh` would *only* support those features in the POSIX standard, but by default let some of their extra features through. The C-style for-loop is not a POSIX feature, but may be in `sh` mode by the actual shell. 2013-09-12T19:41:22.943Z+00:00 This answer is useful 40 Save this answer. Show activity on this post. Step the loop manually: ``` i=0 max=10 while [ $i -lt $max ] do echo "output: $i" true $(( i++ )) done ``` If you don’t have to be totally POSIX, you can use the arithmetic *for* loop: ``` max=10 for (( i=0; i < max; i++ )); do echo "output: $i"; done ``` Or use *jot*(1) on BSD systems: ``` for i in $( jot 0 10 ); do echo "output: $i"; done ``` answered Sep 18, 2009 at 16:17 [![Nietzche-jou's user avatar](https://www.gravatar.com/avatar/2533c49c0597021c8ef573bf08be8528?s=64&d=identicon&r=PG)](https://stackoverflow.com/users/39892/nietzche-jou) [Nietzche-jou](https://stackoverflow.com/users/39892/nietzche-jou) 14\.8k4 gold badges37 silver badges46 bronze badges 3 Comments [![](https://www.gravatar.com/avatar/5e5b6587ba56fcaae86fe5db95248e98?s=48&d=identicon&r=PG)](https://stackoverflow.com/users/194894/flow) `true $(( i++ ))` doesn't work in all cases, so most portable would be `true $((i=i+1))`. 2012-03-22T14:50:01.037Z+00:00 [![](https://i.sstatic.net/3jvVV.jpg?s=64)](https://stackoverflow.com/users/975199/logan) semi colon should not come in "for (( i=0; i \< max; i++ ));" 2014-02-10T13:02:15.42Z+00:00 [![](https://lh3.googleusercontent.com/a-/AOh14Gh77XbK_ZXsqVwrM6a6K6g2DNN4_XD_K1wOopWCVA=k-s48)](https://stackoverflow.com/users/14672114/rare-kpop-manifesto) `jot 0 10` might be an infinite loop starting at 10. To get the same effect as `max=10; for (( i=0; i < max; i++ )); do echo "output: $i"; done` I think the syntax should be `jot -w 'output: %d' 10 0` (directly print out sans loop) or just `for i in $( jot 10 0 ); do …..` for the looped variant 2024-05-26T01:20:19.787Z+00:00 This answer is useful 22 Save this answer. Show activity on this post. If the `seq` command available on your system: ``` for i in `seq 2 $max` do echo "output: $i" done ``` If not, then use poor man's `seq` with `perl`: ``` seq=`perl -e "\$,=' ';print 2..$max"` for i in $seq do echo "output: $i" done ``` Watch those quote marks. answered Sep 18, 2009 at 16:01 [![mob's user avatar](https://www.gravatar.com/avatar/f14e7610fca39322ebbf23530def15f8?s=64&d=identicon&r=PG)](https://stackoverflow.com/users/168657/mob) [mob](https://stackoverflow.com/users/168657/mob) 119k18 gold badges159 silver badges291 bronze badges Comments This answer is useful 19 Save this answer. Show activity on this post. We can iterate loop like as C programming. ``` #!/bin/bash for ((i=1; i<=20; i=i+1)) do echo $i done ``` answered Jul 21, 2018 at 12:07 [![rashedcs's user avatar](https://i.sstatic.net/adedu.jpg?s=64)](https://stackoverflow.com/users/6714430/rashedcs) [rashedcs](https://stackoverflow.com/users/6714430/rashedcs) 3,7632 gold badges43 silver badges44 bronze badges Comments This answer is useful 10 Save this answer. Show activity on this post. There's more than one way to do it. ``` max=10 for i in `eval "echo {2..$max}"` do echo "$i" done ``` answered Sep 18, 2009 at 16:20 [![ephemient's user avatar](https://www.gravatar.com/avatar/09b9758a4a83cc25547eb93891f19df7?s=64&d=identicon&r=PG)](https://stackoverflow.com/users/20713/ephemient) [ephemient](https://stackoverflow.com/users/20713/ephemient) 207k40 gold badges293 silver badges404 bronze badges 2 Comments [![](https://www.gravatar.com/avatar/fa05233b2357f8d11c22ef4cfc7bb85c?s=48&d=identicon&r=PG)](https://stackoverflow.com/users/1126841/chepner) A security risk, since `eval` will evaluate *anything* you set `max` to. Consider `max="2}; echo ha; #"`, then replace `echo ha` with something more destructive. 2013-09-12T19:45:27.713Z+00:00 [![](https://www.gravatar.com/avatar/c0c1d6fa6f329573985fa292569adc24?s=48&d=identicon&r=PG)](https://stackoverflow.com/users/218830/conrad-meyer) (S)he set max to 10. No risk. 2014-04-21T01:13:16.847Z+00:00 This answer is useful 5 Save this answer. Show activity on this post. This is a way: Bash: ``` max=10 for i in $(bash -c "echo {2..${max}}"); do echo $i; done ``` The above Bash way will work for `ksh` and `zsh` too, when `bash -c` is replaced with `ksh -c` or `zsh -c` respectively. Note: `for i in {2..${max}}; do echo $i; done` works in `zsh` and `ksh`. answered Jul 12, 2015 at 12:30 [![Jahid's user avatar](https://i.sstatic.net/XvLrx.jpg?s=64)](https://stackoverflow.com/users/3744681/jahid) [Jahid](https://stackoverflow.com/users/3744681/jahid) 22\.6k10 gold badges97 silver badges114 bronze badges Comments This answer is useful 3 Save this answer. Show activity on this post. Well, as I didn't have the `seq` command installed on my system (Mac OS X v10.6.1 (Snow Leopard)), I ended up using a `while` loop instead: ``` max=5 i=1 while [ $max -gt $i ] do (stuff) done ``` \**Shrugs*\* Whatever works. [![Peter Mortensen's user avatar](https://i.sstatic.net/RIZKi.png?s=64)](https://stackoverflow.com/users/63550/peter-mortensen) answered Sep 18, 2009 at 16:16 [![eykanal's user avatar](https://www.gravatar.com/avatar/8d6be7c07677acaa346e903a497d0acc?s=64&d=identicon&r=PG)](https://stackoverflow.com/users/168775/eykanal) [eykanal](https://stackoverflow.com/users/168775/eykanal) 27\.3k19 gold badges86 silver badges114 bronze badges 1 Comment [![](https://www.gravatar.com/avatar/f979d6d83d40b10d9ad43bdfc51cb1de?s=48&d=identicon&r=PG)](https://stackoverflow.com/users/52963/system-pause) seq is relatively new. I only found out about it a few months ago. But you *can* use a 'for' loop!! The disadvantage of a 'while' is that you have to remember to increment the counter somewhere inside the loop, or else loop downwards. 2009-09-18T16:23:03.563Z+00:00 This answer is useful 2 Save this answer. Show activity on this post. These all do `{1..8}` and should all be POSIX. They also will not break if you put a conditional `continue` in the loop. The canonical way: ``` f= while [ $((f+=1)) -le 8 ] do echo $f done ``` Another way: ``` g= while g=${g}1 [ ${#g} -le 8 ] do echo ${#g} done ``` and another: ``` set -- while set $* . [ ${#} -le 8 ] do echo ${#} done ``` answered Dec 19, 2015 at 18:51 user4427511 1 Comment [![](https://www.gravatar.com/avatar/37c028b8688476f507f012b17f70006d?s=48&d=identicon&r=PG)](https://stackoverflow.com/users/11338291/kkocdko) Wonderful, I'm looking for a solution for busybox's ash, and your answer is perfect 2023-01-27T19:57:03.243Z+00:00 This answer is useful 2 Save this answer. Show activity on this post. Here it worked on Mac OS X. It includes the example of a BSD date, how to increment and decrement the date also: ``` for ((i=28; i>=6 ; i--)); do dat=`date -v-${i}d -j "+%Y%m%d"` echo $dat done ``` [![Peter Mortensen's user avatar](https://i.sstatic.net/RIZKi.png?s=64)](https://stackoverflow.com/users/63550/peter-mortensen) answered Dec 6, 2013 at 5:42 [![minhas23's user avatar](https://i.sstatic.net/Tz7kk.jpg?s=64)](https://stackoverflow.com/users/2458916/minhas23) [minhas23](https://stackoverflow.com/users/2458916/minhas23) 9,7616 gold badges60 silver badges40 bronze badges Comments This answer is useful 1 Save this answer. Show activity on this post. Use: ``` max=10 for i in `eval echo {2..$max}` do echo $i done ``` You need the explicit 'eval' call to reevaluate the {} after variable substitution. [![Peter Mortensen's user avatar](https://i.sstatic.net/RIZKi.png?s=64)](https://stackoverflow.com/users/63550/peter-mortensen) answered Sep 18, 2009 at 16:27 [![Chris Dodd's user avatar](https://www.gravatar.com/avatar/0f02eed6d98a2b824e209ecd7bbddb6a?s=64&d=identicon&r=PG)](https://stackoverflow.com/users/29759/chris-dodd) Comments This answer is useful 0 Save this answer. Show activity on this post. Use `seq 2 10 | xargs printf 'output: %d\n'`, if you only need the output, and the loop may be implicit. answered Dec 15, 2025 at 0:47 [![Jens Jensen's user avatar](https://www.gravatar.com/avatar/eb0885cbf28359cf375207765028afe5?s=64&d=identicon&r=PG)](https://stackoverflow.com/users/1235724/jens-jensen) [Jens Jensen](https://stackoverflow.com/users/1235724/jens-jensen) 1,1281 gold badge10 silver badges22 bronze badges Comments Start asking to get answers Find the answer to your question by asking. [Ask question](https://stackoverflow.com/questions/ask) Explore related questions See similar questions with these tags.
ML Classification
ML Categories
/Computers_and_Electronics
98.9%
/Computers_and_Electronics/Programming
72.8%
/Computers_and_Electronics/Programming/Scripting_Languages
56.1%
/Internet_and_Telecom
12.7%
/Internet_and_Telecom/Web_Services
10.3%
Raw JSON
{
    "/Computers_and_Electronics": 989,
    "/Computers_and_Electronics/Programming": 728,
    "/Computers_and_Electronics/Programming/Scripting_Languages": 561,
    "/Internet_and_Telecom": 127,
    "/Internet_and_Telecom/Web_Services": 103
}
ML Page Types
/Article
83.1%
/Article/FAQ
40.1%
Raw JSON
{
    "/Article": 831,
    "/Article/FAQ": 401
}
ML Intent Types
Informational
99.7%
Raw JSON
{
    "Informational": 997
}
Content Metadata
Languageen
Authornull
Publish Time2009-09-18 15:57:38 (16 years ago)
Original Publish Time2009-09-18 15:57:38 (16 years ago)
RepublishedNo
Word Count (Total)4,198
Word Count (Content)1,404
Links
External Links60
Internal Links197
Technical SEO
Meta NofollowNo
Meta NoarchiveNo
JS RenderedYes
Redirect Targetnull
Performance
Download Time (ms)142
TTFB (ms)114
Download Size (bytes)63,386
Shard169 (laksa)
Root Hash714406497480128969
Unparsed URLcom,stackoverflow!/questions/1445452/shell-script-for-loop-syntax s443