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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T17:36:43+00:00 2026-05-22T17:36:43+00:00

I have a problem updating a value of a variable in a shell script

  • 0

I have a problem updating a value of a variable in a shell script from inside of a while loop. It can be simulated with the following piece of code:

 printf "aaa\nbbb\n" | \
      while read x ; do
          y=$x
          echo "INSIDE: $y"
      done
 echo "OUTSIDE: $y"

Output:

INSIDE: aaa
INSIDE: bbb
OUTSIDE: 

Here printf command just display two lines, while-read loop read it line by line, updating certain variable, but as soon as control going out of the loop the value of the variable gets lost.

I guess the problem is related to the fact that ‘pipe-while-read’ statement causes shell to execute the body of the loop in a subprocess, which cannot update the shell variables in the main loop.

If I rewrite the first two lines of code as

 for x in `printf "aaa\nbbb\n" ` ; do

Output:

INSIDE: aaa
INSIDE: bbb
OUTSIDE: bbb

It could be a workaround, but not for my case because in reality I have not ‘aaa’ and ‘bbb’ but more complex strings including whitespaces etc.

Any idea how to tackle the problem, namely: read a command output line by line in a loop and be able to update shell variables?

Thanks.

  • 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-22T17:36:44+00:00Added an answer on May 22, 2026 at 5:36 pm

    An excerpt from man bash:

    Each command in a pipeline is executed as a separate process (i.e., in a subshell).

    And Subshell cannot change the variable in Parent.

    One of the possible Solution is:

    IFS='\n'
    while read x ; do
       y=${x}
       echo "INSIDE: ${y}"
    done <<EOT
    aaa
    bbb
    EOT
    echo "OUTSIDE: ${y}"
    

    Or if the input is a file:

    IFS='\n'
    while read x ; do
       y=${x}
       echo "INSIDE: ${y}"
    done < /path/to/file
    echo "OUTSIDE: ${y}"
    

    This reads one line at a time, and doesn’t have any issue with spaces.

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

Sidebar

Related Questions

I have a problem with updating the value of property from code which is
I have problem compilin this code..can anyone tell whats wrong with the syntax CREATE
I have a problem with the following implementation of hook_cron in Drupal 6.1.3. The
I have encountered a weird situation while updating/upgrading some legacy code. I have a
I have a problem with Updating Form Element Look and Feel under Update Panel
I have a problem with changing my spring/hibernate application from MySql to SQL Server.
I have a problem updating a foreign key in an Entity Framework entity. I
On our MOSS Enterprise environment, we have a strange problem. From time to time,
I have problem in some JavaScript that I am writing where the Switch statement
I have problem with return statment >.< I want to store all magazine names

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.