27

I have a bash script and I have an else statement which I want it to do nothing.

What is the best way to do this?

2 Answers 2

50

The standard way to do it is using colon ::

if condition; do
  command
else
  :
fi

or true:

if condition; do
  command
else
  true
fi

But why not just skipping the else part:

if condition; do
  command
fi

In zsh and yash, you can even make the else part empty:

if condition; then
  command
else
fi
1
  • "But why not just skipping the else part"? Because sometimes it's handy to have a placeholder when writing a script: remember to do something here!
    – RonJohn
    Commented Apr 12 at 17:04
7

The else statement is optional. Just type:

if [ expr ]; then
#bla
#bla
#bla
fi

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .