{"id":3715,"date":"2017-04-03T17:20:26","date_gmt":"2017-04-03T11:50:26","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=3715"},"modified":"2017-04-03T17:20:26","modified_gmt":"2017-04-03T11:50:26","slug":"how-can-use-multiple-variables-single-loop-shell-script","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/how-can-use-multiple-variables-single-loop-shell-script\/","title":{"rendered":"[ Solved -1 Answers] How can we use multiple variables in single for loop in shell script"},"content":{"rendered":"<p><label class=\"label label-warning\">PROBLEM:<\/label><\/p>\n<ul>\n<li>We implementing as below code using for loop but wrong output coming after running the script.<\/li>\n<\/ul>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">bash code<\/span> <\/div> <pre class=\"language-bash code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-bash code-embed-code\">for i in `awk -F&quot;|&quot; &#039;{print $1}&#039; $INPUTFILE`, j in `awk -F&quot;|&quot; &#039;{print $2}&#039; $INPUTFILE`<br\/>do<br\/>echo $i:$j<br\/>done  <\/code><\/pre> <\/div>\n<p><label class=\"label label-info\">SOLUTION 1:<\/label><\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">bash code<\/span> <\/div> <pre class=\"language-bash code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-bash code-embed-code\">while IFS=&#039;|&#039; read -r i j rest &lt;&amp;3; do<br\/>  {<br\/>    printf &#039;%s\\n&#039; &quot;something with $i and $j&quot;<br\/>  } 3&lt;&amp;-<br\/>done 3&lt; &quot;$INPUTFILE&quot;<\/code><\/pre> <\/div>\n[ad type=&#8221;banner&#8221;]\n<ul>\n<li>But using a shell loop to process text is often the wrong way to go.<\/li>\n<\/ul>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">bash code<\/span> <\/div> <pre class=\"language-bash code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-bash code-embed-code\">awk -F &#039;|&#039; &#039;{print $1 &quot;:&quot; $2}&#039; &lt; &quot;$INPUTFILE&quot;<\/code><\/pre> <\/div>\n<p>Now as an answer to the question in the title, for a shell with for loops taking more than one variables, you&#8217;ve got zsh (you seem to already be using zsh syntax by not quoting your variables or not disabling globbing when splitting command substitution):<\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">bash code<\/span> <\/div> <pre class=\"language-bash code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-bash code-embed-code\">$ for i j in {1..6}; do echo $i:$j; done<br\/>1:2<br\/>3:4<br\/>5:6<\/code><\/pre> <\/div>\n[ad type=&#8221;banner&#8221;]\n<p>Or the shorter form:<\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">bash code<\/span> <\/div> <pre class=\"language-bash code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-bash code-embed-code\">for i j ({1..6} echo $i:$j<\/code><\/pre> <\/div>\n<p>The equivalent with POSIX shells:<\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">bash code<\/span> <\/div> <pre class=\"language-bash code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-bash code-embed-code\">set -- 1 2 3 4 5 6<br\/>## or:<br\/># IFS=&#039;<br\/># &#039; # split on newline<br\/># set -f # disable globbing<br\/># set -- $(awk ...) # split the output of awk or other command<br\/>while [ &quot;$#&quot; -gt 0 ]; do<br\/>  echo &quot;$1:$2&quot;<br\/>  shift 2<br\/>done<\/code><\/pre> <\/div>\n","protected":false},"excerpt":{"rendered":"<p>How can we use multiple variables in single for loop in shell script- implementing as below code using loop but wrong output<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1699],"tags":[7284,7294,7291,7307,7282,7283,7288,7305,7303,7290,7296,7300,7302,7297,7292,7277,7293,7298,7276,7279,7280,7299,7295,7301,7281,7306,7285,7304,7289,7286,7274,7278,7275,7287],"class_list":["post-3715","post","type-post","status-publish","format-standard","hentry","category-linux","tag-advanced-bash-shell-scripting-guide","tag-array-in-bash","tag-array-in-shell-script","tag-array-shell-script","tag-awk-v-multiple-variables","tag-awk-pass-multiple-variables","tag-bash-array","tag-bash-array-example","tag-bash-array-index","tag-bash-array-loop","tag-bash-array-of-strings","tag-bash-associative-array","tag-bash-declare","tag-bash-declare-array","tag-bash-echo","tag-bash-for-i-and-j","tag-bash-iterate-array","tag-bash-loop-array","tag-bash-pass-two-variables-from-file-to-a-for-loop","tag-bash-read-multiple-variables","tag-bash-read-two-variables-from-line","tag-bash-script-array","tag-bash-sequence","tag-echo-bash","tag-for-loop-with-multiple-variables-python","tag-linux-bash-array","tag-making-a-for-loop-with-multiple-variables","tag-shell-array","tag-shell-script-array","tag-shell-script-basics","tag-trying-to-update-php-on-centos-5-7","tag-two-variables-in-for-loop-bash","tag-unable-to-use-yum-repos-whether-with-baseurl-or-mirrorlist-in-centos-6-5","tag-use-multiple-variables-in-single-for-loop-in-shell-script"],"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/3715","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/comments?post=3715"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/3715\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=3715"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=3715"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=3715"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}