Skip to content

Instantly share code, notes, and snippets.

@terenced
Created September 14, 2020 15:27
Show Gist options
  • Save terenced/7ca3871d6e73e8b5199b5dc341e1c430 to your computer and use it in GitHub Desktop.
Save terenced/7ca3871d6e73e8b5199b5dc341e1c430 to your computer and use it in GitHub Desktop.
How to check if a variable is set in bash
+----------------------+------------+-----------------------+-----------------------+
| if VARIABLE is: | set | empty | unset |
+----------------------+------------+-----------------------+-----------------------+
- | ${VARIABLE-default} | $VARIABLE | "" | "default" |
= | ${VARIABLE=default} | $VARIABLE | "" | $(VARIABLE="default") |
? | ${VARIABLE?default} | $VARIABLE | "" | exit 127 |
+ | ${VARIABLE+default} | "default" | "default" | "" |
+----------------------+------------+-----------------------+-----------------------+
:- | ${VARIABLE:-default} | $VARIABLE | "default" | "default" |
:= | ${VARIABLE:=default} | $VARIABLE | $(VARIABLE="default") | $(VARIABLE="default") |
:? | ${VARIABLE:?default} | $VARIABLE | exit 127 | exit 127 |
:+ | ${VARIABLE:+default} | "default" | "" | "" |
+----------------------+------------+-----------------------+-----------------------|
https://stackoverflow.com/questions/3601515/how-to-check-if-a-variable-is-set-in-bash
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment