Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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:

  • This question is quite similar, but specifically asks about solutions in bash.
  • I'm not looking for people to advise me to stay away from csh because it's cursed, scary, or bash is better. I'm specifically interested in a more elegant solution than what I'm using now.
like image 789
Pete Avatar asked Feb 25 '10 18:02

Pete


People also ask

How do I check if an environment variable is set?

In the command window that opens, enter echo %VARIABLE%. Replace VARIABLE with the name of the environment variable you set earlier. For example, to check if MARI_CACHE is set, enter echo %MARI_CACHE%. If the variable is set, its value is displayed in the command window.

How do you check if an environment variable is set or not in bash?

To confirm whether a variable is set or not in Bash Scripting, we can use -v var or -z ${var} options as an expression with the combination of 'if' conditional command.

How do I check environment variables in Linux?

To list all the environment variables, use the command " env " (or " printenv "). You could also use " set " to list all the variables, including all local variables.


1 Answers

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.

like image 134
Jim Lewis Avatar answered Sep 24 '22 09:09

Jim Lewis