<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>therning.org/ magnus &#187; bash</title>
	<atom:link href="http://therning.org/magnus/archives/tag/bash/feed" rel="self" type="application/rss+xml" />
	<link>http://therning.org/magnus</link>
	<description>Incoherent mumblings</description>
	<lastBuildDate>Wed, 17 Mar 2010 00:39:19 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Bash is simply insane</title>
		<link>http://therning.org/magnus/archives/783</link>
		<comments>http://therning.org/magnus/archives/783#comments</comments>
		<pubDate>Thu, 05 Nov 2009 10:31:58 +0000</pubDate>
		<dc:creator>Magnus</dc:creator>
				<category><![CDATA[Posts]]></category>
		<category><![CDATA[bash]]></category>
		<category><![CDATA[scoping]]></category>

		<guid isPermaLink="false">http://therning.org/magnus/?p=783</guid>
		<description><![CDATA[What do you think the following scripti will print?


foo&#40;&#41; &#123;
    true &#124; while true; do
        false
        rc=$?
        if &#91; $rc -eq 1 &#93;; then
        [...]]]></description>
			<content:encoded><![CDATA[<p>What do you think the following script<sup>i</sup> will print?</p>


<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">foo<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span> <span style="color: #7a0874; font-weight: bold;">&#123;</span>
    <span style="color: #c20cb9; font-weight: bold;">true</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #000000; font-weight: bold;">while</span> <span style="color: #c20cb9; font-weight: bold;">true</span>; <span style="color: #000000; font-weight: bold;">do</span>
        <span style="color: #c20cb9; font-weight: bold;">false</span>
        <span style="color: #007800;">rc</span>=<span style="color: #007800;">$?</span>
        <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #007800;">$rc</span> <span style="color: #660033;">-eq</span> <span style="color: #000000;">1</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>; <span style="color: #000000; font-weight: bold;">then</span>
            <span style="color: #7a0874; font-weight: bold;">return</span> <span style="color: #000000;">1</span>
        <span style="color: #000000; font-weight: bold;">fi</span>
    <span style="color: #000000; font-weight: bold;">done</span>
    <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #007800;">$?</span>
    <span style="color: #7a0874; font-weight: bold;">return</span> <span style="color: #000000;">0</span>
<span style="color: #7a0874; font-weight: bold;">&#125;</span>
&nbsp;
foo <span style="color: #000000; font-weight: bold;">||</span> <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;Failed foo&quot;</span></pre></div></div>


<p>Run it and see.  I suspect everyone but the script gurus out there will be surprised.</p>

<p>What about this script then?</p>


<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">bar <span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span> <span style="color: #7a0874; font-weight: bold;">&#123;</span>
    <span style="color: #7a0874; font-weight: bold;">local</span> <span style="color: #007800;">rc</span>=<span style="color: #000000;">0</span>
    <span style="color: #c20cb9; font-weight: bold;">true</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #000000; font-weight: bold;">while</span> <span style="color: #c20cb9; font-weight: bold;">true</span>; <span style="color: #000000; font-weight: bold;">do</span>
        <span style="color: #c20cb9; font-weight: bold;">false</span>
        <span style="color: #007800;">rc</span>=<span style="color: #007800;">$?</span>
        <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #007800;">$rc</span> <span style="color: #660033;">-eq</span> <span style="color: #000000;">1</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>; <span style="color: #000000; font-weight: bold;">then</span>
            <span style="color: #7a0874; font-weight: bold;">return</span> <span style="color: #000000;">1</span>
        <span style="color: #000000; font-weight: bold;">fi</span>
    <span style="color: #000000; font-weight: bold;">done</span>
    <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #007800;">$?</span>
    <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #007800;">$rc</span>
    <span style="color: #7a0874; font-weight: bold;">return</span> <span style="color: #000000;">0</span>
<span style="color: #7a0874; font-weight: bold;">&#125;</span>
&nbsp;
bar</pre></div></div>


<p>Surprised again?</p>

<p>I guess this means that scoping in bash is somewhat more complicated then I would have ever guessed.</p>
<div class='bookmarkify'><a name='bookmarkify'></a><div class='linkbuttons'><a href='http://digg.com/submit?phase=2&amp;url=http://therning.org/magnus/archives/783&amp;title=Bash is simply insane' title='Digg It!' onclick='target="_blank";' rel='nofollow'><img src='http://therning.org/magnus/wp-content/plugins/bookmarkify/digg.png' style='width:16px; height:16px;' alt='[Digg] ' /></a> <a href='http://reddit.com/submit?url=http://therning.org/magnus/archives/783&amp;title=Bash is simply insane' title='Reddit' onclick='target="_blank";' rel='nofollow'><img src='http://therning.org/magnus/wp-content/plugins/bookmarkify/reddit.png' style='width:16px; height:16px;' alt='[Reddit] ' /></a>  <a title='See more bookmark and sharing options...' href='http://therning.org/magnus/archives/783#bookmarkify' rel='nofollow'><small>More&nbsp;&raquo;</small></a></div></div><ol class="footnotes"><li id="footnote_0_783" class="footnote">The script is somewhat artificial, who would ever use the construct <code>true | while ...</code>?  I&#8217;ve used this just to show the point while keeping the examples short.  Feel free to replace that part with something more useful, like <code>cat myfile | while read ...</code>.</li></ol>]]></content:encoded>
			<wfw:commentRss>http://therning.org/magnus/archives/783/feed</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
	</channel>
</rss>
