Skip to main content
Typo, minor rephrasing
Source Link
AdminBee
  • 23.6k
  • 25
  • 55
  • 77

preserve while Preserve read statement's last variable assignments outside of "while" loop

I am trying to echo the variables $f1, $f2, etcetera... , outside of the whilewhile loop shown below. From what I understand, the scope of the variable is not correct. Therefore I tried a few of the workarounds as shown here: http://mywiki.wooledge.org/BashFAQ/024in the Bash FAQ:

while IFS=: read -r f1 f2 f3 f4 f5 f6 f7; do echo "Username: $f1. Home directory:$f6" done</etc/passwd echo $f1 
while IFS=: read -r f1 f2 f3 f4 f5 f6 f7; do echo "Username: $f1. Home directory:$f6" done</etc/passwd echo $f1 

However, I can't seem to reproduce the fix(es) as shown in the link. Can someone provide an applied example on how to fix this issue?

preserve while read statement's last variable assignments outside of loop

I am trying to echo the variables $f1, $f2, etcetera, outside of the while loop shown below. From what I understand, the scope of the variable is not correct. Therefore I tried a few of the workarounds as shown here: http://mywiki.wooledge.org/BashFAQ/024

while IFS=: read -r f1 f2 f3 f4 f5 f6 f7; do echo "Username: $f1. Home directory:$f6" done</etc/passwd echo $f1 

However, I can't seem to reproduce the fix(es) as shown in the link. Can someone provide an applied example on how to fix this issue?

Preserve read statement's last variable assignments outside of "while" loop

I am trying to echo the variables $f1, $f2, ... , outside of the while loop shown below. From what I understand, the scope of the variable is not correct. Therefore I tried a few of the workarounds as shown in the Bash FAQ:

while IFS=: read -r f1 f2 f3 f4 f5 f6 f7; do echo "Username: $f1. Home directory:$f6" done</etc/passwd echo $f1 

However, I can't seem to reproduce the fix(es) as shown in the link. Can someone provide an applied example on how to fix this issue?

more precise question necessary to distinguish it from the many other questions about preserving assignments *inside* the loop - this concerns assignments made by the while read statement itself -- essentially to preserve the final state of the loop assignments - a subtle but important distinction
Link

Variables preserve while read statement's last variable assignments outside of while loop

Tweeted twitter.com/#!/StackUnix/status/625718231639191554
Source Link
User
  • 75
  • 2
  • 7

Variables outside of while loop

I am trying to echo the variables $f1, $f2, etcetera, outside of the while loop shown below. From what I understand, the scope of the variable is not correct. Therefore I tried a few of the workarounds as shown here: http://mywiki.wooledge.org/BashFAQ/024

while IFS=: read -r f1 f2 f3 f4 f5 f6 f7; do echo "Username: $f1. Home directory:$f6" done</etc/passwd echo $f1 

However, I can't seem to reproduce the fix(es) as shown in the link. Can someone provide an applied example on how to fix this issue?