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

  • SEARCH
  • Home
  • 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 7819647
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T07:02:07+00:00 2026-06-02T07:02:07+00:00

Hi guys sorry for the awkward title but I think it is a bit

  • 0

Hi guys sorry for the awkward title but I think it is a bit subtle to describe. So here is my problem, I want to keep a count (i) in a while loop which reads input from awk, and then print the value of i after the loop. However i becomes back to zero after the loop. Below is a simplified version of my program, in reality I also did some string matching inside the loop so that some lines are skipped and i does not increment.

I have tried to remove awk and do another ordinary while loop and i’s value is kept after the loop, therefore I believe it’s not due to some syntax error.

Any idea is great appreciated!

#!/bin/bash
arr=();
i=0;
awk -F '{print $1}' SOMEFILE | while read var
do
  echo $var;
  arr[i]=$var;
  i=$((i+1));
  echo $i;
done
echo $i;
  • 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-02T07:02:15+00:00Added an answer on June 2, 2026 at 7:02 am

    Because the while loop is in a pipeline, it is running as a subprocess, and the value of i is local to that subprocess. There are several ways to keep the value; use a named pipe instead of running while in a pipeline, use process substitution, or use an interpolating heredoc. Here’s an example of the latter:

    while read var; do ... done << EOF
    $( awk ... )
    EOF
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Sorry guys for asking that dumb question but I have problem with that thing.
sorry guys, asked a bit wrongly, here is the EDIT got a simple question,
guys sorry for being so noob but im still starting yet with my php,
Sorry for the wall of text guys but this one requires expliaining, way too
Sorry guys, I'm a noob. I know that some languages support type casting, but
Sorry guys, I hate asking dumb questions but I have seriously been searching for
Sorry guys,just a simple question but I cannot find exact question on google. The
Sorry Guys for all the vague questions. this is what i want to do.
Possible Duplicate: Difference between single quotes and double quotes in Javascript Sorry guys, but
Guys sorry if sounds a little bit naive... In an activity I have one

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.