The while loop in my script is taking input from the variable, which has multiple lines, and processing the variable as one line. How can I get my while loop to process the variable line by line.
I'm looking for an alternative for how I can assign the variable "input", like I do here input=$(tail -n +2 $1)
, or an alternative to how I pass that variable into the while loop, like I do here done <<< $(echo $input)
Script
input=$(tail -n +2 $1)
echo "input: $input :input"
while IFS='' read -r line || [[ -n "$line" ]]; do
echo "output: $line :output"
done <<< $(echo "$input")
Input File ( $1 )
header
test
test2
test3
test4
Output
input: test
test2
test3
test4 :input
output: test test2 test3 test4 :output
Desired Output
input: test
test2
test3
test4 :input
output: test :output
output: test2 :output
output: test3 :output
output: test4 :output
Update:
I executed these commands one after each other, so I don't know whats different on your end that my $(echo "$input")
command isn't passing in the variable line by line
machine:folder user$ cat temp
#!/bin/BASH
input=$(tail -n +2 $1)
echo "input: $input :input"
while IFS='' read -r line || [[ -n "$line" ]]; do
echo "output: $line :output"
done <<< $(echo "$input")
machine:folder user$ cat test
header
test
test2
test3
test4
machine:folder user$ ./temp test
input: test
test2
test3
test4 :input
output: test test2 test3 test4 :output
machine:folder user$ BASH --version
GNU bash, version 4.4.12(1)-release (x86_64-apple-darwin16.5.0)
Copyright (C) 2016 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software; you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.