FactorPad
Faster Learning Tutorials

Linux if then else bash script with examples

if then else allows you to build a branch in a script and create conditional logic (so it is not technically a command but a keyword).
  1. Purpose - Learn what if is for and how to find help.
  2. Options - Review a few common options and arguments.
  3. Examples - Walk through code examples with if.
  4. Script - Add if to our script and run it.
  5. A tip - Finish off with one more insight.
Paul Alan Davis, CFA, November 22, 2016
Updated: August 6, 2018
In this tutorial, 94 of 100, below find a 3-4 minute introductory video, a text-based tutorial and all of the code examples from the video.

Outline Back Next

~/ home  / tech  / linux essentials  / if command


The Linux if command (keyword)

Intermediate

Learn to build a branch in a bash script at the Linux command line.

Video Tutorial

Linux if then else bash script with examples (3:55)

Videos can also be accessed from the YouTube Playlist.

Video Script

The Command and Why You Need It

Our ninety-fourth word, or command to memorize is if from our category Workflow.

if (plus other keywords) allows you to build a branch in a script.

Recall from videos (tutorials) #87 to #93, we're using a script to demonstrate workflow, now we'll introduce if at the command line and then script it.

Before we start, it helps to think of commands as mini programs and most follow this structure: command -option(s) argument(s).

The if command (condition/keyword) has no traditional options and no arguments as it has its own syntax, and our structure here has been command-based, but if is a technically a bash keyword, not a command and I'll explain it with 4 other related keywords: then, else, elif and fi.

Unlike most commands, help is not available with double-dash --help, as the if construct is a shell built-in covered in the bash man page.

So why is if an important command (keyword)? Well, we need to tell bash to follow conditional logic. And now you know how to do that.

Demonstration

Okay, the best way to embed this in your memory is by typing in your own terminal window.

Find this on your Mac using a program called Terminal. On Linux use Terminal or Konsole, and currently Microsoft is adding this functionality to Windows.

Here we go. Let's review the 5 words in this group that I mentioned earlier.

$ type if; type then; type else; type elif; type fi if is a shell keyword then is a shell keyword else is a shell keyword elif is a shell keyword fi is a shell keyword

And as you can see, they all are keywords.

Next, try an if on one line, with integers and incorporate the brackets from that video (tutorial) we just did on test. And we have to assume an input variable here, and let's use myinput=1.

$ myinput=1 $ if [ $myinput -eq 1 ]; then echo "Menu 1"; fi Menu 1 $ _

And then the logic goes, if myinput=1, then input was 1, and your exit status was 0 for true, and it ends there with fi. (At the command line you can use the ; to separate the steps. It always ends with fi and will get there if the condition is true and it will go to the second step, or if false, it will skip the second step and go straight to the third, or fi.)

Next, let's change myinput to 2, and then re-run this line here.

$ myinput=2 $ if [ $myinput -eq 1 ]; then echo "Menu 1"; fi $ _

And it skipped to the fi to end (and had no output) because the exit status was not true (not zero).

And then, since this line is getting very long, let's view it in our script and pause to review this menu and logic for the branches here, and then we'll try it out.

$ nano -c +140 bin/funscript

(Below is the screen from within nano.)

GNU nano 2.2.6 File: /home/factorpad/bin/funscript if [[ "$yourpick" =~ [1-5] ]]; then if [[ $yourpick == 1 ]]; then printf "\n\tYou Selected 1. One moment please...\n" sleep 2s fun_status continue fi if [[ $yourpick == 2 ]]; then printf "\n\tYou selected 2. One moment please...\n" sleep 2s fun_returns continue fi if [[ $yourpick == 3 ]]; then printf "\n\tYou selected 3. One moment please... \n" sleep 2s fun_summary continue fi if [[ $yourpick == 4 ]]; then printf "\n\tYou selected 4.\n" sleep 1s printf "\n\tThank you for your time.\n" sleep 1s printf "\n\tLet's finish up...\n" sleep 2s fun_finish break fi if [[ $yourpick == 5 ]]; then printf "\n\tYou selected 5.\n" sleep 1s echo -e "\n\tThank you, have a nice day." sleep 1s break fi else printf "\n\tInvalid input. Please try again\n" sleep 2s continue fi ^G Get Help ^0 WriteOut ^R Read File ^Y Prev Page ^K Cut Text ^C Cur Pos ^X Exit ^J Justify ^W Where Is ^V Next Page ^U UnCut Text ^T To Spell

(Please note: the code above is meant to illustrate the concept of a multi-level conditional statement. It will not run without other code in the script. Go to the last video #100 to see the code for the whole script if you'd like to try it on your own.)

(Hit Ctrl-x to leave nano and y to confirm saving.)

And last, run it with ~/bin/funscript and here's our progress so far.

$ ~/bin/funscript The current date and time: Mon Nov 22 18:22:42 PST 2016 What is your name? Paul Thank you Paul (several lines trimmed)

(The section below is after the pause and clear screen.)

Please select from the following options: (1) Show system status (2) Collect returns data (3) See a sample of the data (4) Finish the Linux Essentials playlist (5) Quit Your choice: 5 You selected 5. Thank you, have a nice day. $ _

A Final Tip

Okay now you know how to use if. And you know the syntax for commands, options and arguments.

One last tip about the if command (keyword). So at the end there, I left with the #5 to quit because I built a loop, and the first few choices sit in that loop, and I'll explain that next.

Okay, thanks for visiting today. I hope this was a helpful introduction to the if command (keyword).


Learn More About The Series

For an overview of the 100 videos, the 8 quizzes, a cheat sheet, the categories and a Q&A section, visit:


What's Next?

See other scripts and programs built in other languages at our YouTube Channel. Subscribe here.

  • For the Outline to all 100 tutorials, click Outline.
  • To go back to the printf command, hit Back.
  • To learn another important shell script programming concept called a function, click Next.

Outline Back Next

~/ home  / tech  / linux essentials  / if command



 
 
Keywords:
linux if keyword
if else condition
bash if
shell if
bash if else
bash if tutorial
bash script
if bash
linux script
shell script if
if then else elif fi
bash if condition
shell script
bash program logic
shell script example
create branch in bash script
bash if statement
if else in shell script
if condition in shell script
shell scripting
linux if examples
bash if examples