bash: checking if an env var is set? - Help

This is a discussion on bash: checking if an env var is set? - Help ; How do I check if an environment variable is set?...

+ Reply to Thread
Results 1 to 7 of 7

Thread: bash: checking if an env var is set?

  1. bash: checking if an env var is set?

    How do I check if an environment variable is set?

  2. Re: bash: checking if an env var is set?

    On Thu, 19 Oct 2006 17:35:27 +0300, Angel Tsankov wrote:
    > How do I check if an environment variable is set?


    Well doing a
    man test
    would suggest you can test string for nonzero length or if length is zero.


  3. Re: bash: checking if an env var is set?

    Angel Tsankov wrote:
    > How do I check if an environment variable is set?


    ${foo+1}

    If $foo is unset this expands to NULL
    If $foo is set (including set to NULL) this will expand to 1

    If NULL and unset are equivalent use ${foo:+1}

    /dan

  4. Re: bash: checking if an env var is set?

    Angel Tsankov wrote:
    >
    > How do I check if an environment variable is set?


    If programatically, use getenv() in C. It returns NULL if not
    found. From N869 (C standard):

    7.20.4.5 The getenv function

    Synopsis

    [#1]

    #include
    char *getenv(const char *name);

    Description

    [#2] The getenv function searches an environment list,
    provided by the host environment, for a string that matches
    the string pointed to by name. The set of environment names
    and the method for altering the environment list are
    implementation-defined.

    [#3] The implementation shall behave as if no library
    function calls the getenv function.

    Returns

    [#4] The getenv function returns a pointer to a string
    associated with the matched list member. The string pointed
    to shall not be modified by the program, but may be
    overwritten by a subsequent call to the getenv function. If
    the specified name cannot be found, a null pointer is
    returned.

    --
    Chuck F (cbfalconer at maineline dot net)
    Available for consulting/temporary embedded and systems.



  5. Re: bash: checking if an env var is set?

    On 2006-10-19, Angel Tsankov wrote:
    > How do I check if an environment variable is set?


    [ -n "${VAR+x}" ] ## Fails if VAR is unset

    [ -n "${VAR:+x}" ] ## Fails if VAR is unset or empty

    [ -n "${VAR-x}" ] ## Succeeds if VAR is unset

    [ -n "${VAR:-x}" ] ## Succeeds if VAR is unset or empty

    --
    Chris F.A. Johnson, author |
    Shell Scripting Recipes: | My code in this post, if any,
    A Problem-Solution Approach | is released under the
    2005, Apress | GNU General Public Licence

  6. Re: bash: checking if an env var is set?

    Angel Tsankov wrote:
    > How do I check if an environment variable is set?


    echo $VARIABLE

  7. Re: bash: checking if an env var is set?

    Angel Tsankov wrote:
    > How do I check if an environment variable is set?

    shell:~$ env

    That will list the environment variables.

+ Reply to Thread