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 6587391
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T16:54:40+00:00 2026-05-25T16:54:40+00:00

Consider the following script: #!/bin/bash num=0 cat file | while read line; do echo

  • 0

Consider the following script:

#!/bin/bash

num=0
cat file | while read line; do
    echo "$line"
    lines[$num]="$line"
    ((num++))
    echo "num = $num"
done

echo "end num = $num"

i=0
while [ $i -lt $num ]; do
    echo "${lines[$i]}"
    ((i++))
done

Normally, it should read the file line by line, store the result in an array, then go through the array and print it line by line. The problem is that the variable $num resets somehow after the first loop exits. The output of this script for me is the following (using a file with some random garbage in it):

dsfkljhhsdfsdfshdjkfgd
num = 1
fdfgdfgdfg
num = 2
dfgdfgdfgdfg
num = 3
dfgdfgdfgdfgdfg
num = 4
dfgdfgdfgdfgdfgd
num = 5
fgdfgdfgdfg
num = 6
dfgdfgdfgdfg
num = 7
dfgdfgdfgdfgdfg
num = 8
dfgdfgdfgdfg
num = 9
dfgdfgdgdgdg
num = 10
dfgdffgdgdgdg
num = 11
end num = 0

Why is this? How do I achieve to remember the variable? I am using bash 3.1.17 on SUSE Linux 10.

  • 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-05-25T16:54:41+00:00Added an answer on May 25, 2026 at 4:54 pm

    Why? It’s because this:

    cat file | while read line; do
        echo "$line"
        lines[$num]="$line"
        ((num++))
        echo "num = $num"
    done
    

    runs the while statement in a separate process, with its own environment, not touching the parent environment. You’ll find, similarly, that the lines array is not there either.

    The following simplified script shows this in action:

    #!/bin/bash
    export xyzzy=42
    echo urk | while read line; do
        xyzzy=999
        echo $xyzzy
    done
    echo $xyzzy
    

    The output of that script is:

    999
    42
    

    because the setting of the variable to 999 is done in the subprocess.

    Bottom line, if you want information to be reflected in the current process (the script), you’ll need to do the work in the script or find some other way to get the information out of the sub-process.

    If you use input redirection rather than starting a sub-process pipeline, it should work as you want. That’s because the while bit is then done in the context of the current process rather than a separate process in a pipeline. For example:

    #!/bin/bash
    export xyzzy=42
    while read line; do
        xyzzy=999
        echo $xyzzy
    done <<EOF
    hello
    EOF
    echo $xyzzy
    

    will produce:

    999
    999
    

    For your specific case, replace:

    done <<EOF
    hello
    EOF
    

    with:

    done <file
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider the following: me@mine:~$ cat a.sh #!/bin/bash echo Lines: $LINES echo Columns: $COLUMNS me@mine:~$
Consider the following perl script ( read.pl ): my $line = <STDIN>; print Perl
Consider the following script: use IO::File; $| = 1; my ($handle, $pid) = myPipe();
Consider the following PowerShell script: function Alpha { # write-output 'Uncomment this line and
Consider two web pages with the following in their body respectively: <body> <script> document.writeln('<textarea></textarea>')
Consider the following script: println ls -l.execute().text Why do I get the following error
Consider the following Perl code. #!/usr/bin/perl use strict; use warnings; $b=1; my $a=${b}; $b=2;
Consider the following shell script: gzip -dc in.gz | sed -e 's/@/_at_/g' | gzip
Consider the following XSLT script: <?xml version=1.0 encoding=ISO-8859-1?> <xsl:stylesheet version=1.0 xmlns:xsl=http://www.w3.org/1999/XSL/Transform> <xsl:output method=text encoding=iso-8859-1/>
Consider following script (it's total nonsense in pseudo-language): if (Request.hostMatch(asfasfasf.com) && someString.existsIn(new String[] {brr,

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.