Correct Notation Replacement Process :
while read i; do echo $i; done < <(echo "$FILECONTENT")
The last value i assigned in the loop is available when the loop ends. An alternative is:
echo $FILECONTENT | { while read i; do echo $i; done ...do other things using $i here... }
Brackets are an I / O grouping operation and do not create a subshell themselves. In this context, they are part of the pipeline and therefore run as a subshell, but this is due to | , not with { ... } . You mentioned this in a question. AFAIK, you can make a return from within the function.
Bash also provides shopt built-in and one of its many options:
lastpipe
If set and job control is not active, the shell runs the last pipeline command that is not running in the background in the current shell environment.
Thus, using this type in a script makes modfied sum available after the loop:
FILECONTENT="12 Name 13 Number 14 Information" shopt -s lastpipe # Comment this out to see the alternative behaviour sum=0 echo "$FILECONTENT" | while read number name; do ((sum+=$number)); done echo $sum
Performing this on the command line is usually performed with the error "task management is inactive" (that is, task control is active on the command line). Testing this without using a script failed.
Also, as Gareth Rhys pointed out in his answer, you can sometimes use the line here :
while read i; do echo $i; done <<< "$FILECONTENT"
This does not require shopt ; You can save the process using it.
Jonathan Leffler Oct 24 '13 at 16:07 2013-10-24 16:07
source share