0

What I want is simply to do something if the first variable of the script is empty, in a case statement.

Unfortunately I spent yet a certain amount of time searching for the solution but didn't find it.

Here is my code:

case $1 in 
    [[ -z $1 ]]) echo "something" ;; 
esac
2

You don't need test command when using case, and don't need case when using test:

case $1 in 
  ("") echo "something" ;; 
esac

and:

[[ -z $1 ]] && echo "something"

or using old test [...] for portability:

[ -z "$1" ] && echo "something"
2
  • Thx a lot, it works now :p
    – math
    May 22 '15 at 15:17
  • @math Please note the double quotes with the "old" test. Some shells return an error when the variable after -z is empty unless you use the quotes.
    – Walter A
    May 22 '15 at 21:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.