0
#intro
if [ -n "$1" ]
    then
        echo 666
    else
        echo 555
fi
exit;

Actually I do want to echo 555 while I don't want to do anything in the first block, what should I do? I noticed that I can't just remove echo 666.

AGamePlayer
  • 7,605

3 Answers3

6

Just using no-op:

if [ -n "$1" ]; then
  :
else
  echo 555
fi
exit

or invert the logic:

if [ -z "$1" ]; then
  echo 555
fi
exit
cuonglm
  • 153,898
3
[ -n "$1" ] && echo "666"  <= For if part
[ -n "$1" ] || echo "555"  <= For else part
SHW
  • 14,786
  • 14
  • 66
  • 101
3

You can invert an if statement by using ! (this is called the 'not' operator, which means take the opposite of the result) inside the conditional. So that [ -n "$1" ] becomes:[ ! -n "$1" ], which is the same as the else section.

It's also valid to use the -z option instead of -n which is logically inverse to -n, but as a general rule the ! will always match whatever the else section would match.

Centimane
  • 4,490