<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	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/"
		>
<channel>
	<title>Comments on: Bash: test for undefined variable</title>
	<atom:link href="http://www.willmer.com/kb/2007/08/bash-test-for-undefined-variable/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.willmer.com/kb/2007/08/bash-test-for-undefined-variable/</link>
	<description></description>
	<lastBuildDate>Sun, 15 Aug 2010 05:18:19 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: Niranjan</title>
		<link>http://www.willmer.com/kb/2007/08/bash-test-for-undefined-variable/comment-page-1/#comment-45106</link>
		<dc:creator>Niranjan</dc:creator>
		<pubDate>Mon, 25 Jan 2010 13:20:26 +0000</pubDate>
		<guid isPermaLink="false">http://www.willmer.com/kb/2007/08/bash-test-for-undefined-variable/#comment-45106</guid>
		<description>A VAR could take on teh following values:

VAR=
VAR=&quot;&quot; (Same as above)
VAR=&quot; &quot;, and
VAR=&quot;something&quot; (which is same as #3, VAR=&quot; &quot;)

The solution has to defend against all these options. 

Assuming (Big assumption) that VAR&#039;s value is always greater than a single alpha-numeric, one can test for the length of VAR.</description>
		<content:encoded><![CDATA[<p>A VAR could take on teh following values:</p>
<p>VAR=<br />
VAR=&#8221;" (Same as above)<br />
VAR=&#8221; &#8220;, and<br />
VAR=&#8221;something&#8221; (which is same as #3, VAR=&#8221; &#8220;)</p>
<p>The solution has to defend against all these options. </p>
<p>Assuming (Big assumption) that VAR&#8217;s value is always greater than a single alpha-numeric, one can test for the length of VAR.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jochen</title>
		<link>http://www.willmer.com/kb/2007/08/bash-test-for-undefined-variable/comment-page-1/#comment-38925</link>
		<dc:creator>Jochen</dc:creator>
		<pubDate>Fri, 05 Jun 2009 20:16:05 +0000</pubDate>
		<guid isPermaLink="false">http://www.willmer.com/kb/2007/08/bash-test-for-undefined-variable/#comment-38925</guid>
		<description>Ugh of course I cut off the bottom line in the paste.

$ export SOMEVAR=
$ defined &quot;${SOMEVAR} &#124;&#124; echo &quot;notset&quot;
notset</description>
		<content:encoded><![CDATA[<p>Ugh of course I cut off the bottom line in the paste.</p>
<p>$ export SOMEVAR=<br />
$ defined &#8220;${SOMEVAR} || echo &#8220;notset&#8221;<br />
notset</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jochen</title>
		<link>http://www.willmer.com/kb/2007/08/bash-test-for-undefined-variable/comment-page-1/#comment-38924</link>
		<dc:creator>Jochen</dc:creator>
		<pubDate>Fri, 05 Jun 2009 20:15:08 +0000</pubDate>
		<guid isPermaLink="false">http://www.willmer.com/kb/2007/08/bash-test-for-undefined-variable/#comment-38924</guid>
		<description>Not it doesn&#039;t suffice. 

$ defined â€œ${SOMEVAR}â€ &#124;&#124; echo â€œnotsetâ€
notset
$ export SOMEVAR=â€sometextâ€
$ defined â€œ${SOMEVAR}â€ &#124;&#124; echo â€œnotsetâ€
$ export SOMEVAR=
$ defined â€œ${SOMEVAR}â€ &#124;&#124; echo â€œnotsetâ€

It&#039;ll show &quot;notset&quot; when a variable is defined but empty.

-z only tells you whether a variable has content, not whether it&#039;s set or not.</description>
		<content:encoded><![CDATA[<p>Not it doesn&#8217;t suffice. </p>
<p>$ defined â€œ${SOMEVAR}â€ || echo â€œnotsetâ€<br />
notset<br />
$ export SOMEVAR=â€sometextâ€<br />
$ defined â€œ${SOMEVAR}â€ || echo â€œnotsetâ€<br />
$ export SOMEVAR=<br />
$ defined â€œ${SOMEVAR}â€ || echo â€œnotsetâ€</p>
<p>It&#8217;ll show &#8220;notset&#8221; when a variable is defined but empty.</p>
<p>-z only tells you whether a variable has content, not whether it&#8217;s set or not.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Panos</title>
		<link>http://www.willmer.com/kb/2007/08/bash-test-for-undefined-variable/comment-page-1/#comment-37368</link>
		<dc:creator>Panos</dc:creator>
		<pubDate>Fri, 01 May 2009 12:55:22 +0000</pubDate>
		<guid isPermaLink="false">http://www.willmer.com/kb/2007/08/bash-test-for-undefined-variable/#comment-37368</guid>
		<description>This suffices:

defined() {
        [[ ! -z &quot;${1}&quot; ]]
}

$ defined &quot;${SOMEVAR}&quot; &#124;&#124; echo &quot;notset&quot;
notset
$ export SOMEVAR=&quot;sometext&quot;
$ defined &quot;${SOMEVAR}&quot; &#124;&#124; echo &quot;notset&quot;</description>
		<content:encoded><![CDATA[<p>This suffices:</p>
<p>defined() {<br />
        [[ ! -z "${1}" ]]<br />
}</p>
<p>$ defined &#8220;${SOMEVAR}&#8221; || echo &#8220;notset&#8221;<br />
notset<br />
$ export SOMEVAR=&#8221;sometext&#8221;<br />
$ defined &#8220;${SOMEVAR}&#8221; || echo &#8220;notset&#8221;</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Sven</title>
		<link>http://www.willmer.com/kb/2007/08/bash-test-for-undefined-variable/comment-page-1/#comment-31502</link>
		<dc:creator>Sven</dc:creator>
		<pubDate>Tue, 25 Nov 2008 22:23:35 +0000</pubDate>
		<guid isPermaLink="false">http://www.willmer.com/kb/2007/08/bash-test-for-undefined-variable/#comment-31502</guid>
		<description>Thanks,  this helped me a lot!</description>
		<content:encoded><![CDATA[<p>Thanks,  this helped me a lot!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Rob</title>
		<link>http://www.willmer.com/kb/2007/08/bash-test-for-undefined-variable/comment-page-1/#comment-30373</link>
		<dc:creator>Rob</dc:creator>
		<pubDate>Wed, 22 Oct 2008 21:06:35 +0000</pubDate>
		<guid isPermaLink="false">http://www.willmer.com/kb/2007/08/bash-test-for-undefined-variable/#comment-30373</guid>
		<description>It works even better with some double quotes.  Try this:

defined()
{
  [ &quot;${!1-one} == &quot;${!1-two}&quot; ]
}

By way of explanation, it uses three bash expansions.  First, you get the arguments to a function via ${1}.  Second, you get indirection by adding the bang.  ${!a} returns the value of the variable named by a.  Third, it uses default values, just like your original code.  I usually use &quot;one&quot; and &quot;two&quot; instead of &quot;X&quot; and &quot;Y&quot;.  It doesn&#039;t mater, so long as the values are different.

(I wasn&#039;t the previous commiter.  I&#039;m very indebted to both the original post and the previous comment.  It was very helpful to me!)</description>
		<content:encoded><![CDATA[<p>It works even better with some double quotes.  Try this:</p>
<p>defined()<br />
{<br />
  [ "${!1-one} == "${!1-two}" ]<br />
}</p>
<p>By way of explanation, it uses three bash expansions.  First, you get the arguments to a function via ${1}.  Second, you get indirection by adding the bang.  ${!a} returns the value of the variable named by a.  Third, it uses default values, just like your original code.  I usually use &#8220;one&#8221; and &#8220;two&#8221; instead of &#8220;X&#8221; and &#8220;Y&#8221;.  It doesn&#8217;t mater, so long as the values are different.</p>
<p>(I wasn&#8217;t the previous commiter.  I&#8217;m very indebted to both the original post and the previous comment.  It was very helpful to me!)</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: ChrisGarrod</title>
		<link>http://www.willmer.com/kb/2007/08/bash-test-for-undefined-variable/comment-page-1/#comment-29552</link>
		<dc:creator>ChrisGarrod</dc:creator>
		<pubDate>Tue, 07 Oct 2008 18:00:22 +0000</pubDate>
		<guid isPermaLink="false">http://www.willmer.com/kb/2007/08/bash-test-for-undefined-variable/#comment-29552</guid>
		<description>Thanks for both bits of info.  I really like function defined, but don&#039;t understand how it works. !1 ?? very curious.  By the way, on my BASH, only BASH_VERSINFO is an array:

# 10:56:58 garrod@sioprism 2.15 $ cd ~ &amp;&amp; set &#124; grep BASH
BASH=/bin/bash
BASH_VERSINFO=([0]=&quot;2&quot; [1]=&quot;05b&quot; [2]=&quot;0&quot; [3]=&quot;1&quot; [4]=&quot;release&quot; [5]=&quot;ia64-redhat-linux-gnu&quot;)
BASH_VERSION=&#039;2.05b.0(1)-release&#039;</description>
		<content:encoded><![CDATA[<p>Thanks for both bits of info.  I really like function defined, but don&#8217;t understand how it works. !1 ?? very curious.  By the way, on my BASH, only BASH_VERSINFO is an array:</p>
<p># 10:56:58 garrod@sioprism 2.15 $ cd ~ &amp;&amp; set | grep BASH<br />
BASH=/bin/bash<br />
BASH_VERSINFO=([0]=&#8221;2&#8243; [1]=&#8221;05b&#8221; [2]=&#8221;0&#8243; [3]=&#8221;1&#8243; [4]=&#8221;release&#8221; [5]=&#8221;ia64-redhat-linux-gnu&#8221;)<br />
BASH_VERSION=&#8217;2.05b.0(1)-release&#8217;</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Anonymous</title>
		<link>http://www.willmer.com/kb/2007/08/bash-test-for-undefined-variable/comment-page-1/#comment-25991</link>
		<dc:creator>Anonymous</dc:creator>
		<pubDate>Wed, 23 Jul 2008 20:32:41 +0000</pubDate>
		<guid isPermaLink="false">http://www.willmer.com/kb/2007/08/bash-test-for-undefined-variable/#comment-25991</guid>
		<description>defined()
{
  [ ${!1-X} == ${!1-Y} ]
}

Example:
$ defined FOO &#124;&#124; echo notset
notset
$ FOO=X
$ defined FOO &#124;&#124; echo notset
$ FOO=
$ defined FOO &#124;&#124; echo notset
$ unset FOO
$ defined FOO &#124;&#124; echo notset
notset
$ defined BASH_VERSION[2] &#124;&#124; echo notset
$ defined BASH_VERSION[20] &#124;&#124; echo notset
notset
$</description>
		<content:encoded><![CDATA[<p>defined()<br />
{<br />
  [ ${!1-X} == ${!1-Y} ]<br />
}</p>
<p>Example:<br />
$ defined FOO || echo notset<br />
notset<br />
$ FOO=X<br />
$ defined FOO || echo notset<br />
$ FOO=<br />
$ defined FOO || echo notset<br />
$ unset FOO<br />
$ defined FOO || echo notset<br />
notset<br />
$ defined BASH_VERSION[2] || echo notset<br />
$ defined BASH_VERSION[20] || echo notset<br />
notset<br />
$</p>
]]></content:encoded>
	</item>
</channel>
</rss>
