Skip to content

Bash Tricks

Here is a bunch of neat little tricks that you can do with bash

Display message if $VAR is not defined

While referring to variables in bash you can use the notation ${VAR:?message}
This will makes sure that if $VAR is not defined then the message thing will be used/displayed.

Here is a small example illustrating this feature.

~ ❯❯❯ echo "${TEST_VAR?not defined; please check and try again}"                                                                                                                       
zsh: TEST_VAR: not defined; please check and try again
~ ❯❯❯ TEST_VAR="Hello my friend"                                                                                                                                                       
~ ❯❯❯ echo "${TEST_VAR?not defined; please check and try again}"                                                                                                                       
Hello my friend