{"id":1575,"date":"2011-07-20T09:12:00","date_gmt":"2011-07-20T09:12:00","guid":{"rendered":"http:\/\/www.b.shuttle.de\/hayek\/Hayek\/Jochen\/wp\/blog-en\/2011\/07\/20\/shell-programming-for-loops-vs-while-loops-and-scope-of-variables-created-within-loop-bodies\/"},"modified":"2011-07-20T09:12:00","modified_gmt":"2011-07-20T09:12:00","slug":"shell-programming-for-loops-vs-while-loops-and-scope-of-variables-created-within-loop-bodies","status":"publish","type":"post","link":"https:\/\/wp.jochen.hayek.name\/blog-en\/2011\/07\/20\/shell-programming-for-loops-vs-while-loops-and-scope-of-variables-created-within-loop-bodies\/","title":{"rendered":"shell programming: &#8220;for&#8221; loops vs. &#8220;while&#8221; loops and scope of variables created within loop bodies"},"content":{"rendered":"<div>Variables, that you create (by assignment) within loop bodies, are well visible outside the loop body. This is valid for both kinds of loop.<\/p>\n<div>\n<\/div>\n<div>\nCAVEAT:<\/div>\n<div>\nIf the &#8220;while&#8221; loop reads from a pipe, the &#8220;while&#8221; loop is executed within a subshell, i.e. the scope of the variables within the loop ends with the loop.<\/div>\n<div>\n<\/div>\n<div>\nExample:<\/div>\n<blockquote><p>\n<span>x=a<br \/>\necho hello |<br \/>\nwhile read l<br \/>\ndo<br \/>\u00a0 x=b<br \/>\ndone<br \/>\necho &#8220;x=$x&#8221;<br \/>\n<\/span><\/p><\/blockquote>\n<div>\n<\/div>\n<div>\nWhat&#8217;s the output of this <span>echo<\/span>?<\/div>\n<blockquote><p>\n<span>x=a<\/span><\/p><\/blockquote>\n<div>\nExample:<\/p>\n<blockquote><p>\n<span>x=a<br \/>for l in hello<br \/>do<br \/>\u00a0 x=b<br \/>done<br \/>echo &#8220;x=$x&#8221;<\/span><\/p>\n<div>\n<span><br \/><\/span><\/div>\n<\/blockquote>\n<div>\nWhat&#8217;s the output of this <span>echo<\/span>?<\/div>\n<blockquote><p>\n<span>x=b<\/span><\/p><\/blockquote>\n<p><\/p>\n<div>\n<\/div>\n<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Variables, that you create (by assignment) within loop bodies, are well visible outside the loop body. This is valid for both kinds of loop. CAVEAT: If the &#8220;while&#8221; loop reads from a pipe, the &#8220;while&#8221; loop is executed within a subshell, i.e. the scope of the variables within the loop ends with the loop. Example: [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_crdt_document":"","jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"_share_on_mastodon":"0"},"categories":[636],"tags":[],"class_list":["post-1575","post","type-post","status-publish","format-standard","hentry","category-the-shell-programming-language"],"share_on_mastodon":{"url":"","error":""},"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/paO0kP-pp","jetpack_likes_enabled":true,"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/wp.jochen.hayek.name\/blog-en\/wp-json\/wp\/v2\/posts\/1575","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/wp.jochen.hayek.name\/blog-en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/wp.jochen.hayek.name\/blog-en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/wp.jochen.hayek.name\/blog-en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/wp.jochen.hayek.name\/blog-en\/wp-json\/wp\/v2\/comments?post=1575"}],"version-history":[{"count":0,"href":"https:\/\/wp.jochen.hayek.name\/blog-en\/wp-json\/wp\/v2\/posts\/1575\/revisions"}],"wp:attachment":[{"href":"https:\/\/wp.jochen.hayek.name\/blog-en\/wp-json\/wp\/v2\/media?parent=1575"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/wp.jochen.hayek.name\/blog-en\/wp-json\/wp\/v2\/categories?post=1575"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/wp.jochen.hayek.name\/blog-en\/wp-json\/wp\/v2\/tags?post=1575"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}