Possible Duplicate:Why is my variable being localized in one while read loop, but not in another seemingly similar loop I have a bash script that scraps URLs to feed a MySQL database. In my loop, I have a variable called query that gets filled ok, but when I try to output it, it always says it's empty!#!/bin/bash#   get path to here[ -L $0 ] && P1=$(readlink $0) || P1=$0P2=$(dirname $P1)#   slugify the name of the city into URL formatfunction slugify() {    slug=$@    echo    $slug             |    tr      '[A-Z]' '[a-z]'     |    sed     's/\s/-/g'}echo Getting list of cities from csv filei=$(( 0 ))      #   lines readg=$(( 0 ))      #   curl OKf=$(( 0 ))      #   number of pages not found#   total number of cities in CSV filelines=$(wc -l  $P2/docs/cities.csv | cut -d' ' -f1)#   Reading CSV file line by linewhile read ROWdo    # getting city iso_code    city_iso_code=$(echo $ROW | cut -d, -f1| sed -e 's/.$//' | sed 's/^FR\([^A-Z0-9]\+\)//')    # getting city name    city_name=$(echo $ROW | cut -d, -f3 | sed s/'/\\\'/g | sed -e 's/.$//')    # stop after 1 good result to see if everything ok    # [[ $city_name == A* ]] || {    [ $g -gt 1 ] && {        echo 'Done for today'        # THIS IS WHERE query is empty when it should not!!        echo Query: <$query>        exit    }    # slugify city name    city_slug=$(slugify $city_name)    # construct url to fetch    city_url=http://******/****$city_slug.html    # visual teller    echo -en \r$g ok, $f not found | $city_name | $city_url                # check if page exists, if so scrap it, else fill the Not found page    # I realize I am doing 2 curl Calls for every request :(    [ $(curl -s -o /dev/null -w %{http_code} $city_url) -eq 200 ] && {        # increment the total of OK        g=$(( g + 1 ))        # getting contents and scrapping it        curl 2>/dev/null $city_url  |             sed     's/</\n/g'      |             grep    'td id=r'      |             cut     -d'>' -f 2      |             sed     s/'/\\\'/g    |             while read street            do                # constructing query                query=$query insert into cc_streets (city_iso_code,street_name) values ('FR $city_iso_code','$street');            done        } || {            # not found            f=$(( f + 1 ))            notfound=$notfoundFR $city_iso_code,$city_name,$city_url    }     i=$(( i + 1 ))done < $P2/docs/cities.csvSo in the WHILE loop,when I echo 'Done for today', and try to output the variable query, it says it's empty, but I know the query variable gets filled alright if I echo query in the second WHILE, so query gets reset at one moment in the loop but I can't quite get when. Anybody has a clue as why it does not work? Thanks for your insight!
question about a 'while' logics error
bash