Learn to build a branch in a bash script at the Linux command line.
Linux if then else bash script with examples (3:55)
Videos can also be accessed from the YouTube Playlist.
Our ninety-fourth word, or command to memorize is
if from our category
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
Before we start, it helps to think of commands as mini programs and
most follow this structure:
command -option(s) argument(s).
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
a technically a
bash keyword, not a
command and I'll explain it with 4 other related keywords:
Unlike most commands, help is not available with double-dash
--help, as the
if construct is a shell built-in
covered in the
So why is
if an important command (keyword)?
Well, we need to tell
bash to follow
And now you know how to do that.
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.
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
test. And we have to assume
an input variable here, and let's use
And then the logic goes, if
then input was 1, and your exit status was 0 for true, and it ends there
fi. (At the command line you can
; 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
Next, let's change
myinput to 2, and
then re-run this line here.
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.
(Below is the screen from within
(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.)
Ctrl-x to leave
y to confirm saving.)
And last, run it with
~/bin/funscript and here's our
progress so far.
(The section below is after the pause and clear screen.)
Okay now you know how to use
And you know the syntax for commands, options and arguments.
One last tip about the
(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
For an overview of the 100 videos, the 8 quizzes, a cheat sheet, the categories and a Q&A section, visit:
See other scripts and programs built in other languages at our YouTube Channel. Subscribe here.
printfcommand, hit Back.
function, click Next.