Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7783737
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T19:55:33+00:00 2026-06-01T19:55:33+00:00

Consider the following: for i in 1 2 3; do echo $(( j +=

  • 0

Consider the following:

for i in 1 2 3; do echo $(( j += 1 ))& done

According to (my reading of) the sh language spec, section 2.3 paragraph 5, the arithmetic expansion of j += 1 should take place during token recognition, and should thus be processed before the shell ever reads the &. So it seems that executing the above line should increment j by 3 and each invocation of echo should get a different argument. (Which is the behavior if ‘&’ is replaced by ‘;’). In bash 3.2.25, j is not modified. Is this a bug in bash, or am I misunderstanding something?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T19:55:34+00:00Added an answer on June 1, 2026 at 7:55 pm

    That entire token recognition section deals with parsing, not expansion or command evaluation of any kind. Have a look at the “introduction” for context – parsing comes before basically everything, while evaluation of the arithmetic expression is usually one of the very last evaluation steps. It isn’t really relevant here.

    You’re probably actually wondering about whether expansions occur before the subshell forks for asynchronous lists. They don’t.

     $ ksh93 -c 'typeset -i n; while ((++j%10)); do { n+=1; printf "$n "; } & done; while ((++j%10)); do : $((n++)) ${ printf "$n " >&2;} & done 2>&1; echo'
    1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
     $
     $ bash -c 'f() { printf "#%d %s, %s\n" "$@"; }; f 1 $BASHPID $$; f 2 $BASHPID $$ & sleep 1'      
    #1 12275, 12275
    #2 12276, 12275
     $
    

    As for the order of expansions, arithmetic expansion is performed at the same time as parameter expansion and command substitution, from left-to-right (also as shown above in the second loop).

    I think you’re not the only one to misread that section, I’ve filed a number of bugs related to it which have turned out to be either an error on my part or due to some unfortunate interaction with non-POSIX shell extensions. IMHO that section is too terse.

    If you think there’s a problem with the spec language or the implementations you’ll likely find better answers on one of the mailing lists. ast-users, help-bash, austin group lists

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider the following code: class myclass { function __construct(&$arg1, &$arg2) { echo $arg1; echo
Consider the following php code used for HTML output: echo <div id='logo2'> <a href='if(checkLanguage())
Consider the following: <?php //daytime_check.php $is_daytime = false; if ($is_daytime) { echo '1'; }
Consider following script: SET LANGUAGE 'German' GO SET DATEFIRST 1 GO DECLARE @FullDate DATETIME
Consider the following if(!count($_POST)) { echo 'something'; } if(empty($_POST)) { echo 'something'; } if(!$_POST)
Consider following tables: How to skip and take groups from the table? Tried using
Consider the following: me@mine:~$ cat a.sh #!/bin/bash echo Lines: $LINES echo Columns: $COLUMNS me@mine:~$
Consider the following string and regex: $string= just/RB convinced/VBN closing/VBG 10dma/NN need/VBN see/VB; echo
Consider the following: $ echo index.html* | xargs -L 1 ls -l -rw-r--r-- 1
Consider the following code: echo $this->Form->year('expiry',date('Y'),date('Y')+2) This produces output something like: 2013 2012 2011

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.