{"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[pastacode lang=\u201dbash\u201d manual=\u201dfor%20i%20in%20%60awk%20-F%22%7C%22%20\u2019%7Bprint%20%241%7D\u2019%20%24INPUTFILE%60%2C%20j%20in%20%60awk%20-F%22%7C%22%20\u2019%7Bprint%20%242%7D\u2019%20%24INPUTFILE%60%0Ado%0Aecho%20%24i%3A%24j%0Adone%20%20%0A\u201d message=\u201dbash code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<p><label class=\"label label-info\">SOLUTION 1:<\/label><\/p>\n[pastacode lang=\u201dbash\u201d manual=\u201dwhile%20IFS%3D\u2019%7C\u2019%20read%20-r%20i%20j%20rest%20%3C%263%3B%20do%0A%20%20%7B%0A%20%20%20%20printf%20\u2019%25s%5Cn\u2019%20%22something%20with%20%24i%20and%20%24j%22%0A%20%20%7D%203%3C%26-%0Adone%203%3C%20%22%24INPUTFILE%22%0A\u201d message=\u201dbash code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n[ad type=\u201dbanner\u201d]\n<ul>\n<li>But using a shell loop to process text is often the wrong way to go.<\/li>\n<\/ul>\n[pastacode lang=\u201dbash\u201d manual=\u201dawk%20-F%20\u2019%7C\u2019%20\u2019%7Bprint%20%241%20%22%3A%22%20%242%7D\u2019%20%3C%20%22%24INPUTFILE%22%0A\u201d message=\u201dbash code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<p>Now as an answer to the question in the title, for a shell with for loops taking more than one variables, you\u2019ve 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[pastacode lang=\u201dbash\u201d manual=\u201d%24%20for%20i%20j%20in%20%7B1..6%7D%3B%20do%20echo%20%24i%3A%24j%3B%20done%0A1%3A2%0A3%3A4%0A5%3A6%0A\u201d message=\u201dbash code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n[ad type=\u201dbanner\u201d]\n<p>Or the shorter form:<\/p>\n[pastacode lang=\u201dbash\u201d manual=\u201dfor%20i%20j%20(%7B1..6%7D%20echo%20%24i%3A%24j%0A\u201d message=\u201dbash code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<p>The equivalent with POSIX shells:<\/p>\n[pastacode lang=\u201dbash\u201d manual=\u201dset%20\u2013%201%202%203%204%205%206%0A%23%23%20or%3A%0A%23%20IFS%3D\u2019%0A%23%20\u2019%20%23%20split%20on%20newline%0A%23%20set%20-f%20%23%20disable%20globbing%0A%23%20set%20\u2013%20%24(awk%20\u2026)%20%23%20split%20the%20output%20of%20awk%20or%20other%20command%0Awhile%20%5B%20%22%24%23%22%20-gt%200%20%5D%3B%20do%0A%20%20echo%20%22%241%3A%242%22%0A%20%20shift%202%0Adone%0A\u201d message=\u201dbash code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\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}]}}