Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

249
Views
Why shell does not set a variable which is piped?

I have a variable which i am trying to set something like this:

#!/bin/sh

found=0
id=1
echo "Hello" |
while [ $id != 5 ]
do
      id=`expr $id + 1`
      echo $id
      found=1
done

echo "found = $found" // I expect this to be 1

Why, and how to set this value? I am forced to use like this (piped), because the actual code in production environment is:

found=0
id=1
my_mount_name="/opt/insiteone/fuse-mount"
echo "select file_system_id, mount_name from SystemTable" | mysql ifm -uroot -pinsite3 |
while read file_system_id mount_name
do
   if [ "$id" == "$file_system_id" -a "$my_mount_name" == "$mount_name" ]; then
      echo "Match found for file system ID and mount name"
      found=1
   fi
done
echo "found = $found" // I expect this to be 1, when a match, but does not
over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

The pipe runs in a subshell. You can do a few things to make it work, the simplest is:

found=0
id=1
my_mount_name="/opt/insiteone/fuse-mount"
echo "select file_system_id, mount_name from SystemTable" | 
mysql ifm -uroot -pinsite3 | {
while read file_system_id mount_name
do
   if [ "$id" == "$file_system_id" -a "$my_mount_name" == "$mount_name" ]; then
      echo "Match found for file system ID and mount name"
      found=1
   fi
done
echo "found = $found"; }
# Note the enclosing {}. Inside the black, the variable $found is set.
# After this comment, it will be zero.

This technique may require that the enclosing block be fairly large, so you may want to refactor the rest of the script to make this usable. Another option is to use a fifo or to put the echo/mysql pipeline into a process substitution. (The latter is not portable, but works in bash which may be adequate.) However, in this particular case, it is probably better to do something like:

found=0
id=1
my_mount_name="/opt/insiteone/fuse-mount"

echo "select file_system_id, mount_name from SystemTable" |
mysql ifm -uroot -pinsite3 | {
while read file_system_id mount_name
do
   if [ "$id" == "$file_system_id" -a "$my_mount_name" == "$mount_name" ]; then
      echo "Match found for file system ID and mount name"
      exit 0  # Exit the subshell succesfully
   fi
done
exit 1; } && found=1
over 4 years ago · Santiago Trujillo Report

0

You can pass the variables to the sub shell's environemnt:

foo=1
bar=2

echo "sds" | foo="$foo" bar="$bar" while ... 
over 4 years ago · Santiago Trujillo Report

0

As @fedorqui commented, bash puts pipeline components in separate subshells, so the changes to your variables disappear when the subshell exits.

There are 2 strategies to deal with this:

  1. only use the changed variables in the same subshell

    echo "Hello" |
    {
        while [ $id != 5 ]
        do
            ((id++))
            echo $id
            found=1
        done
        echo "found = $found" // I expect this to be 1
    }
    

    This can be a problem if you have lots of code that relies on those variables

  2. replace the pipeline with process substitution. This means that the while loop is not executed in a subshell, it's run in the current shell:

    while [ $id != 5 ]
    do
        ((id++))
        echo $id
        found=1
    done < <(echo "Hello")
    echo "found = $found" // I expect this to be 1
    

    This can suffer from poor readability, but you can put as many newlines inside <(...) as you want.

    Your production code rewritten with process substitution (and bash/ksh conditional syntax):

    found=0
    id=1
    my_mount_name="/opt/insiteone/fuse-mount"
    
    while read file_system_id mount_name; do
        if [[ $id == $file_system_id ]] && [[ $my_mount_name == $mount_name ]]; then
            echo "Match found for file system ID and mount name"
            found=1
        fi
    done < <(
        echo "select file_system_id, mount_name from SystemTable" | 
        mysql ifm -uroot -pinsite3 
    )
    echo "found = $found" 
    
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!