0

please could you help me how can I use value in variable number. This variable get value in while cycle and it is in subshell. So question is how can I use it in shell. number=0 #blablabla

cat $WEDI_RC | while read line
    do  
       number=$(echo $line | grep $1 | awk -F'[ ]' '{print $3}')
       echo "in cycle"
       echo $number
    done
       echo "after while"
       echo $number
       ((number++))
       echo $number
       echo $1 `date +"%T"` $number >>$WEDI_RC
1

1 Answer 1

1

This is because the pipe creates a subshell, which has it's own counting.

The question has surely be answered many times, so for the full explanation just the link to the bash FAQ:

http://mywiki.wooledge.org/BashFAQ/024

The usual workaround is to redirect input from the input file instead of piping it (and you'll even get rid of the useless use of cat):

while read -r line
do
    ...
done < "$WEDI_RC"
...
Sign up to request clarification or add additional context in comments.

1 Comment

thank you, I have to rewrite last line to "done < $WEDI_RC" and now is everything working well.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.