Linux Tutorial Series

Linux Tutorial Series – 187 – Elif (else if)

Here is the video version, if you prefer it:

Checking only one condition in a conditional is not the only thing we can do. We can check multiple conditions, using elif. Here is how to use elif:


if [ "$1" = ‘Hello’ ]


echo 'Hello back to you!'

elif [ "$1" = ‘Hi’ ]


echo 'Hi!'


echo 'You are rude.'


This is our entire shell script with an added elif, modeled after (Ward, 2014)⁠. Let’s look at its output:

mislav@mislavovo-racunalo:~/Linux_folder$ ./ Hello

Hello back to you!

mislav@mislavovo-racunalo:~/Linux_folder$ ./ Hi


mislav@mislavovo-racunalo:~/Linux_folder$ ./

You are rude.

You can use elif to test for multiple conditions. But there is also another construct, called case, which is more appropriate than just a bunch of elifs. We will talk about case later on.

Hope you learned something new!


Ward, B. (2014). How Linux Works: What Every Superuser Should Know (2nd ed.). No Starch Press. Page 258