[CLUE-Tech] bash question

Keith Hellman kehellman at yahoo.com
Sun Jun 16 12:02:50 MDT 2002


--- Sean Reifschneider <jafo at tummy.com> wrote:
> On Fri, Jun 14, 2002 at 10:57:24AM -0700, Keith Hellman wrote:
> >Does anyone know how to determine if bash environmental variable
> exists?
> 
> You probably want to read the "Parameter Expansion" section of the bash
> man
> page.  For example:
> 
>    guin:jafo$ echo ${unsetvarname:-Hey, this variable is unset}
>    Hey, this variable is unset
>    guin:jafo$ ${unsetvarname:?Unset variable}
>    bash: unsetvarname: Unset variable
>    guin:jafo$ echo $?

No, these do not distinguish between empty vars and non-existing vars;
which is the test I want.  My impression is that '${:[-?]}' is a
convenient shorthand for 'test -z "${VAR}" && VAR="Unset variable"'

The solution I found was typeset -p:

khellman at linux:~> set |grep KEH
khellman at linux:~> echo "${KEH:-unset variable}"
unset variable
khellman at linux:~> KEH=""
khellman at linux:~> set |grep KEH
KEH=
khellman at linux:~> echo "${KEH:-unset variable}"
unset variable
khellman at linux:~> typeset -p KEH && echo $?
declare -- KEH=""
0
khellman at linux:~> unset KEH
khellman at linux:~> typeset -p KEH && echo $?
bash: typeset: KEH: not found
khellman at linux:~> echo $?
1
khellman at linux:~>





=====
Keith E. Hellman
kehellman at yahoo.com

__________________________________________________
Do You Yahoo!?
Yahoo! - Official partner of 2002 FIFA World Cup
http://fifaworldcup.yahoo.com



More information about the clue-tech mailing list