1

I have global variable $MY_VAR.

How can I execute second command only if the variable has value:

echo $MY_VAR && echo "Show me only if variable $MY_VAR has value"

  • 1
    Depends a bit on if you want to test if it's defined (but possibly empty), or if it has a nonempty value. [Testing if a variable is empty in a shell script](https://unix.stackexchange.com/questions/32180/testing-if-a-variable-is-empty-in-a-shell-script), [How can I test if a variable is empty or contains only spaces?](https://unix.stackexchange.com/questions/146942/how-can-i-test-if-a-variable-is-empty-or-contains-only-spaces), [How do I check if a variable exists in an 'if' statement?](https://unix.stackexchange.com/questions/212183/how-do-i-check-if-a-variable-exists-in-an-if-statement) – ilkkachu Apr 11 '21 at 16:07
  • And also: http://mywiki.wooledge.org/BashGuide/TestsAndConditionals – ilkkachu Apr 11 '21 at 16:07
  • @ilkkachu Thanks for info! So there is no way to test if variable exists and it's not empty in command line? I don't need a bash script. Just want to execute a command if variable exists and it's not empty – artnikbrothers Apr 11 '21 at 16:10
  • 1
    The links @ilkkachu posted showed that there are several ways of doing what you want. e,g, `echo $MYVAR ; [ -n "$MYVAR" ] && echo "show me only if..."` – cas Apr 11 '21 at 16:27

0 Answers0