Learn to create a script within a script at the Linux command line.
Linux bash shell script function with examples (3:43)
Videos can also be accessed from the YouTube Playlist.
Our ninety-fifth word, or command (keyword) to memorize is
function from our category
function allows you to create a
script within a script.
Recall from videos (tutorials) #87 to #94, we're building a script to
demonstrate workflow, and now we'll introduce
function and then see them in a
Before we start, it helps to think of commands as mini programs and
most follow this structure:
command -option(s) argument(s).
function command (keyword) has
no traditional options and no arguments as it has its
own syntax, and like
if covered in
video (tutorial) #94, a
bash keyword, and not a
Unlike most commands, help is not available with double-dash
--help, as the
function is a shell built-in
covered in the
man page. Because of their
multi-line structure, these are coded in scripts only.
So why is
function an important command (keyword)?
Well, we writing scripts within scripts is essential.
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. To confirm, let's quickly use
We can see that it is a keyword.
And then now, let's view our script here, and we're using
(Below is the screen from within
First, functions are placed above primary code. That's kind of normal here with functions. Above the code in the script, they are not acted on until called later within the script. That is an important point I wanted to make.
Next, functions come in two equivalent forms. First, using
function, a name, space, open
curly-brace, then indented you see commands and finally a right curly
brace here closes it all out (see function #1 above). And you have an
empty line after that.
Then the second form uses, a name simply, then open and close parentheses, left curly brace, the indented code block and the same close that you saw before (see function #2 above).
(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, because we can't tackle this in 3 minutes, don't sweat the code here, but tune in to the next video (tutorial) to see how it all plays out.
Okay now you know how to use
And you know the syntax for commands, options and arguments.
One last tip about the
(keyword). So after a few more videos (tutorials) and some practice,
you will be a programmer. Isn't that exciting?
Okay, thanks for visiting today. I hope this was a helpful
introduction to the
function command (keyword).
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.
ifstatement, hit Back.
whilekeyword helps us create a program loop, click Next.