[ Solved -1 Answers] How can we use multiple variables in single for loop in shell script
We implementing as below code using for loop but wrong output coming after running the script.
bash code
for i in `awk -F"|" '{print $1}' $INPUTFILE`, j in `awk -F"|" '{print $2}' $INPUTFILE` do echo $i:$j done
bash code
while IFS='|' read -r i j rest <&3; do { printf '%s\n' "something with $i and $j" } 3<&- done 3< "$INPUTFILE"
[ad type=”banner”]
But using a shell loop to process text is often the wrong way to go.
bash code
awk -F '|' '{print $1 ":" $2}' < "$INPUTFILE"
Now as an answer to the question in the title, for a shell with for loops taking more than one variables, you’ve got zsh (you seem to already be using zsh syntax by not quoting your variables or not disabling globbing when splitting command substitution):
bash code
$ for i j in {1..6}; do echo $i:$j; done 1:2 3:4 5:6
[ad type=”banner”]
Or the shorter form:
bash code
for i j ({1..6} echo $i:$j
The equivalent with POSIX shells:
bash code
set -- 1 2 3 4 5 6 ## or: # IFS=' # ' # split on newline # set -f # disable globbing # set -- $(awk ...) # split the output of awk or other command while [ "$#" -gt 0 ]; do echo "$1:$2" shift 2 done
Wikitechy Founder, Author, International Speaker, and Job Consultant. My role as the CEO of Wikitechy, I help businesses build their next generation digital platforms and help with their product innovation and growth strategy. I'm a frequent speaker at tech conferences and events.
superb