Learner
Learner

Reputation: 21445

Checking for environment variable

Using this UNIX script I am able to check if variable TEST_VAR is set or not:

: ${TEST_VAR:?"Not set or empty."}

I am new to unix so can someone please explain what is this command.

Upvotes: 0

Views: 51

Answers (2)

TheEwook
TheEwook

Reputation: 11117

It is the original shell comment notation (before '#' to end of line). For a long time, Bourne shell scripts had a colon as the first character. The C Shell would read a script and use the first character to determine whether it was for the C Shell (a '#' hash) or the Bourne shell (a ':' colon). Then the kernel got in on the act and added support for '#!/path/to/program' and the Bourne shell got '#' comments, and the colon convention went by the wayside

Have a look at this similar question:

What's a concise way to check that environment variables are set in a Unix shell script?

Upvotes: 1

plesiv
plesiv

Reputation: 7028

From bash manual:

${parameter:?word}

If parameter is null or unset, the expansion of word (or a message to that effect if word is not present) is written to the standard error and the shell, if it is not interactive, exits. Otherwise, the value of parameter is substituted.

Upvotes: 1

Related Questions