Pete
Pete

Reputation: 10680

csh idioms to check for environment variable existence?

I've got a few csh scripts where I need to check that certain environment variables are set before I start doing stuff, so I do this sort of thing:

if ! $?STATE then
    echo "Need to set STATE"
    exit 1
endif

if ! $?DEST then
    echo "Need to set DEST"
    exit 1
endif

which is a lot of typing. Is there a more elegant idiom for checking whether or not an environment variable is already set?

Notes:

Upvotes: 32

Views: 29664

Answers (2)

David Thomas Sr.
David Thomas Sr.

Reputation: 1

Try the following:

[ -z STATE ] && echo "Need to set STATE"

[ ! -z DEST  ] && echo "Need to set STATE"

Upvotes: -7

Jim Lewis
Jim Lewis

Reputation: 45115

I think the way you're doing it (an if statement with a condition using the $?VAR syntax, which evaluates to 1 if the variable is set, and 0 otherwise) is probably the most idiomatic csh construct that does what you want.

Upvotes: 22

Related Questions