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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T02:18:30+00:00 2026-05-25T02:18:30+00:00

GNU bash, version 4.1.5(1)-release-(i486-pc-linux-gnu) Ubuntu Linux 10.04 This prints var as 1234 and does

  • 0

GNU bash, version 4.1.5(1)-release-(i486-pc-linux-gnu)
Ubuntu Linux 10.04

This prints var as 1234 and does not print var2

#!/bin/bash

var=1234

while var=9876 var2=765 read line
do

    echo $var $var2
    echo $line

done <datafile

echo out of loop $var

o/p

1234
This is line 1
1234
This is line 2
1234
This is line 3
1234
This is line 4
1234
This is line 5
1234
This is line 6
1234

out of loop 1234

This prints var as 9876 and also prints var2

#!/bin/bash

var=1234

while 
var=9876 
var2=765 
read line
do

    echo $var $var2
    echo $line

done <datafile

echo out of loop $var

o/p

9876 765
This is line 1
9876 765
This is line 2
9876 765
This is line 3
9876 765
This is line 4
9876 765
This is line 5
9876 765
This is line 6
9876 765

out of loop 9876

Why is that?

  • 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-25T02:18:30+00:00Added an answer on May 25, 2026 at 2:18 am

    Edit I see now that your first script IS producing output of 1234. This is happening because var1 is scoped in the main script BUT the read is silently consuming var1 and var2 because they are scoped locally to just the line var=9876 var2=764 read … as I describe below.

    mostly orig post : Per old school unix, a command-line can contain pre-pended variable assignments whose scope is only the command immediately following.

    So your line

    while var=9876 var2=765 read line
          ^--prepened Vars--^cmd^args
    

    is parsed like the ^--- indicate.

    Because the pre-pended vars belong only to the command they are ‘attached’ to, in this case read, they are only visible to that process. (read is probably an internal command, this may be part of the problem).

    By contrast, your second block of code,

    while 
    var=9876 
    var2=765 
    read line
    

    each var assignment is executed inside the current shell, and they do not ‘belong’ to the read command becuase they are not exported.

    Thanks for the OS and bash version, but this is ‘functioning as designed’ for bash, ksh, zsh, and probably others. If you just want to know why this is happening for intellectual curiosity, then you’ll need to spend time reading advanced bash scripting. If this represents a real problem for your code, then you’ll have to post a new question with more details about what you are trying to accomplish.

    I hope this helps.

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

Sidebar

Related Questions

I'm using GNU bash, version 3.00.15(1)-release (x86_64-redhat-linux-gnu). And this command: echo -e doesn't print
I am running GNU bash, version 3.2.39(1)-release (x86_64-pc-linux-gnu). I have a specific question pertaining
Using GNU bash (version 4.0.35(1)-release (x86_64-suse-linux-gnu), I would like to negate a test with
Here is what I use: - MacOS X 10.6.7 - GNU bash, version 3.2.48(1)-release
The GNU/Linux version of cp has a nice --update flag: -u, --update copy only
Bundler version 1.0.0.beta.2 Rvm Info system: uname: Linux dane-r1f-ubuntu 2.6.32-22-generic #33-Ubuntu SMP Wed Apr
I am using GNU/Linux (Ubuntu + Gnome). I have been using netbeans on windows
GNU sed version 4.1.5 seems to fail with International chars. Here is my input
What GNU/Linux command-line tool would I use for performing a search and replace on
When running a bash script on ubuntu 9.10, I get different behavior from bash

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.