<?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: How to check if a Bash variable is set or not</title>
	<atom:link href="http://www.techpulp.com/blog/2009/01/how-to-check-if-a-bash-variable-is-set-or-not/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.techpulp.com/blog/2009/01/how-to-check-if-a-bash-variable-is-set-or-not/</link>
	<description>The pulp of technology</description>
	<lastBuildDate>Sat, 12 May 2012 22:10:13 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
	<item>
		<title>By: Zakhar</title>
		<link>http://www.techpulp.com/blog/2009/01/how-to-check-if-a-bash-variable-is-set-or-not/#comment-208</link>
		<dc:creator>Zakhar</dc:creator>
		<pubDate>Thu, 06 Oct 2011 21:36:27 +0000</pubDate>
		<guid isPermaLink="false">http://techpulp.com/?p=512#comment-208</guid>
		<description>This does not work when used in combination with the very useful switch -u

That is because if you try to do a variable expansion on an unset variable, you will get trapped by the -u, because you are precisely trying to use the variable before setting it.

I&#039;ve a workaround for my case, as I wanted to test that for parameters, and I can simply use the parameter count $# instead, but I still haven&#039;t found a solution compliant with -u</description>
		<content:encoded><![CDATA[<p>This does not work when used in combination with the very useful switch -u</p>
<p>That is because if you try to do a variable expansion on an unset variable, you will get trapped by the -u, because you are precisely trying to use the variable before setting it.</p>
<p>I&#8217;ve a workaround for my case, as I wanted to test that for parameters, and I can simply use the parameter count $# instead, but I still haven&#8217;t found a solution compliant with -u</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Grahack</title>
		<link>http://www.techpulp.com/blog/2009/01/how-to-check-if-a-bash-variable-is-set-or-not/#comment-189</link>
		<dc:creator>Grahack</dc:creator>
		<pubDate>Wed, 13 Apr 2011 20:18:58 +0000</pubDate>
		<guid isPermaLink="false">http://techpulp.com/?p=512#comment-189</guid>
		<description>Hi all,
is there a way to use
if [ -n &quot;${MYVAR+x}&quot; ];
with the name of MYVAR stored in another var?
Something with ${!...}, but I cannot make it work.
Thanks.</description>
		<content:encoded><![CDATA[<p>Hi all,<br />
is there a way to use<br />
if [ -n "${MYVAR+x}" ];<br />
with the name of MYVAR stored in another var?<br />
Something with ${!&#8230;}, but I cannot make it work.<br />
Thanks.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: liz</title>
		<link>http://www.techpulp.com/blog/2009/01/how-to-check-if-a-bash-variable-is-set-or-not/#comment-98</link>
		<dc:creator>liz</dc:creator>
		<pubDate>Sat, 18 Dec 2010 06:15:27 +0000</pubDate>
		<guid isPermaLink="false">http://techpulp.com/?p=512#comment-98</guid>
		<description>If you like to know how it can be done using &quot;awk&quot; command, try following:

[liz@techpulp ~]# echo &quot;Hello, Baby&quot; &#124; awk &#039;{print toupper($0);}&#039;
HELLO, BABY
[liz@techpulp ~]# echo &quot;Hello, Baby&quot; &#124; awk &#039;{print tolower($0);}&#039;
hello, baby
[liz@techpulp ~]#</description>
		<content:encoded><![CDATA[<p>If you like to know how it can be done using &#8220;awk&#8221; command, try following:</p>
<p>[liz@techpulp ~]# echo &#8220;Hello, Baby&#8221; | awk &#8216;{print toupper($0);}&#8217;<br />
HELLO, BABY<br />
[liz@techpulp ~]# echo &#8220;Hello, Baby&#8221; | awk &#8216;{print tolower($0);}&#8217;<br />
hello, baby<br />
[liz@techpulp ~]#</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: liz</title>
		<link>http://www.techpulp.com/blog/2009/01/how-to-check-if-a-bash-variable-is-set-or-not/#comment-97</link>
		<dc:creator>liz</dc:creator>
		<pubDate>Sat, 18 Dec 2010 06:14:47 +0000</pubDate>
		<guid isPermaLink="false">http://techpulp.com/?p=512#comment-97</guid>
		<description>[liz@techpulp ~]$ echo &quot;Hello, Baby&quot; &#124; sed -e &#039;s/./\U&amp;\E/g&#039;
HELLO, BABY
[liz@techpulp ~]$</description>
		<content:encoded><![CDATA[<p>[liz@techpulp ~]$ echo &#8220;Hello, Baby&#8221; | sed -e &#8216;s/./\U&amp;\E/g&#8217;<br />
HELLO, BABY<br />
[liz@techpulp ~]$</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Harry</title>
		<link>http://www.techpulp.com/blog/2009/01/how-to-check-if-a-bash-variable-is-set-or-not/#comment-96</link>
		<dc:creator>Harry</dc:creator>
		<pubDate>Sat, 18 Dec 2010 06:09:59 +0000</pubDate>
		<guid isPermaLink="false">http://techpulp.com/?p=512#comment-96</guid>
		<description>Can some one explain how we can do it using &quot;sed&quot; command</description>
		<content:encoded><![CDATA[<p>Can some one explain how we can do it using &#8220;sed&#8221; command</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Sara Mithal</title>
		<link>http://www.techpulp.com/blog/2009/01/how-to-check-if-a-bash-variable-is-set-or-not/#comment-93</link>
		<dc:creator>Sara Mithal</dc:creator>
		<pubDate>Sat, 18 Dec 2010 05:52:06 +0000</pubDate>
		<guid isPermaLink="false">http://techpulp.com/?p=512#comment-93</guid>
		<description>The following appears function helps in determining if a variable is set or not.

isset()
{
    [ ${!1-X} == ${!1-Y} ]
}

[sara@mypc ~]# isset MYVAR &#124;&#124; echo notset
notset
[sara@mypc ~]# MYVAR=X
[sara@mypc ~]# isset MYVAR &#124;&#124; echo notset
[sara@mypc ~]# MYVAR=
[sara@mypc ~]# isset MYVAR &#124;&#124; echo notset
[sara@mypc ~]# unset MYVAR
[sara@mypc ~]# isset MYVAR &#124;&#124; echo notset
notset
[sara@mypc ~]# isset BASH_VERSION[2] &#124;&#124; echo notset
[sara@mypc ~]# isset BASH_VERSION[20] &#124;&#124; echo notset
notset
[sara@mypc ~]#</description>
		<content:encoded><![CDATA[<p>The following appears function helps in determining if a variable is set or not.</p>
<p>isset()<br />
{<br />
    [ ${!1-X} == ${!1-Y} ]<br />
}</p>
<p>[sara@mypc ~]# isset MYVAR || echo notset<br />
notset<br />
[sara@mypc ~]# MYVAR=X<br />
[sara@mypc ~]# isset MYVAR || echo notset<br />
[sara@mypc ~]# MYVAR=<br />
[sara@mypc ~]# isset MYVAR || echo notset<br />
[sara@mypc ~]# unset MYVAR<br />
[sara@mypc ~]# isset MYVAR || echo notset<br />
notset<br />
[sara@mypc ~]# isset BASH_VERSION[2] || echo notset<br />
[sara@mypc ~]# isset BASH_VERSION[20] || echo notset<br />
notset<br />
[sara@mypc ~]#</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Harry</title>
		<link>http://www.techpulp.com/blog/2009/01/how-to-check-if-a-bash-variable-is-set-or-not/#comment-92</link>
		<dc:creator>Harry</dc:creator>
		<pubDate>Sat, 18 Dec 2010 05:43:35 +0000</pubDate>
		<guid isPermaLink="false">http://techpulp.com/?p=512#comment-92</guid>
		<description>Similar logic can be used to test if a particular environment variable is set or not. All environment variables are directly accessible in a bash script as variables. For example, if you need value of HOME environment variable, you can directly refer $HOME in the bash shell script.</description>
		<content:encoded><![CDATA[<p>Similar logic can be used to test if a particular environment variable is set or not. All environment variables are directly accessible in a bash script as variables. For example, if you need value of HOME environment variable, you can directly refer $HOME in the bash shell script.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Harry</title>
		<link>http://www.techpulp.com/blog/2009/01/how-to-check-if-a-bash-variable-is-set-or-not/#comment-91</link>
		<dc:creator>Harry</dc:creator>
		<pubDate>Sat, 18 Dec 2010 05:40:18 +0000</pubDate>
		<guid isPermaLink="false">http://techpulp.com/?p=512#comment-91</guid>
		<description>Various comparisons that can be used:

[ -n &quot;${VAR+x}&quot; ] # Fails if VAR is unset

[ -n &quot;${VAR:+x}&quot; ] # Fails if VAR is unset or empty

[ -n &quot;${VAR-x}&quot; ] # Succeeds if VAR is unset

[ -n &quot;${VAR:-x}&quot; ] # Succeeds if VAR is unset or empty</description>
		<content:encoded><![CDATA[<p>Various comparisons that can be used:</p>
<p>[ -n "${VAR+x}" ] # Fails if VAR is unset</p>
<p>[ -n "${VAR:+x}" ] # Fails if VAR is unset or empty</p>
<p>[ -n "${VAR-x}" ] # Succeeds if VAR is unset</p>
<p>[ -n "${VAR:-x}" ] # Succeeds if VAR is unset or empty</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Harry</title>
		<link>http://www.techpulp.com/blog/2009/01/how-to-check-if-a-bash-variable-is-set-or-not/#comment-90</link>
		<dc:creator>Harry</dc:creator>
		<pubDate>Sat, 18 Dec 2010 05:37:47 +0000</pubDate>
		<guid isPermaLink="false">http://techpulp.com/?p=512#comment-90</guid>
		<description>I guess the focus is not about assigning default value. 

The following lists possible modifiers:

Modifiers can be applied to the ${name} form of parameter substitution:

${name:-word}
if  name  is set and not null, it is substituted, otherwise word is substituted.

${name:+word}
if name is set and not  null,  word  is  substituted,  otherwise nothing is substituted.

${name:=word}
if  name is set and not null, it is substituted, otherwise it is assigned word and the resulting value of name is substituted.

${name:?word}
if name is set and not null, it is substituted,  otherwise  word is  printed  on  standard error (preceded by name:) and an error occurs (normally causing termination of a shell script, function or  .-script). If word is omitted the string ?parameter null or not set? is used instead.

In the above modifiers, the : can be omitted, in which case the  conditions  only  depend on name being set (as opposed to set and not null).

If word is needed, parameter, command, arithmetic and  tilde  substitution are performed on it; if word is not needed, it is not evaluated.</description>
		<content:encoded><![CDATA[<p>I guess the focus is not about assigning default value. </p>
<p>The following lists possible modifiers:</p>
<p>Modifiers can be applied to the ${name} form of parameter substitution:</p>
<p>${name:-word}<br />
if  name  is set and not null, it is substituted, otherwise word is substituted.</p>
<p>${name:+word}<br />
if name is set and not  null,  word  is  substituted,  otherwise nothing is substituted.</p>
<p>${name:=word}<br />
if  name is set and not null, it is substituted, otherwise it is assigned word and the resulting value of name is substituted.</p>
<p>${name:?word}<br />
if name is set and not null, it is substituted,  otherwise  word is  printed  on  standard error (preceded by name:) and an error occurs (normally causing termination of a shell script, function or  .-script). If word is omitted the string ?parameter null or not set? is used instead.</p>
<p>In the above modifiers, the : can be omitted, in which case the  conditions  only  depend on name being set (as opposed to set and not null).</p>
<p>If word is needed, parameter, command, arithmetic and  tilde  substitution are performed on it; if word is not needed, it is not evaluated.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: cjames</title>
		<link>http://www.techpulp.com/blog/2009/01/how-to-check-if-a-bash-variable-is-set-or-not/#comment-46</link>
		<dc:creator>cjames</dc:creator>
		<pubDate>Fri, 16 Oct 2009 19:23:47 +0000</pubDate>
		<guid isPermaLink="false">http://techpulp.com/?p=512#comment-46</guid>
		<description>&gt;&gt; There is no direct method in bash to determine if a variable is set or not.

Really?

See snippet from the bash website below ...

${parameter=default}, ${parameter:=default}

If parameter not set, set it to default.

Both forms nearly equivalent. The : makes a difference only when $parameter has been declared and is null, [1] as above.</description>
		<content:encoded><![CDATA[<p>&gt;&gt; There is no direct method in bash to determine if a variable is set or not.</p>
<p>Really?</p>
<p>See snippet from the bash website below &#8230;</p>
<p>${parameter=default}, ${parameter:=default}</p>
<p>If parameter not set, set it to default.</p>
<p>Both forms nearly equivalent. The : makes a difference only when $parameter has been declared and is null, [1] as above.</p>
]]></content:encoded>
	</item>
</channel>
</rss>

