/ factorpad.com / tech / linux-essentials / function-command.html
An ad-free and cookie-free website.
Intermediate
Learn to create a script within a script at the Linux command line.
Videos can also be accessed from the Linux Essentials Playlist on YouTube.
Linux bash Shell Script function with Examples (3:43)
Our ninety-fifth word, or command (keyword) to memorize is
function
from our category
Workflow.
function
allows you to create a
script within a script.
-options | description |
---|---|
|
None for the function keyword |
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
script.
Before we start, it helps to think of commands as mini programs and
most follow this structure:
command -option(s) argument(s)
.
The 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 function
is
technically a bash
keyword, and not a
command.
Unlike most commands, help is not available with double-dash
--help
, as the
function
is a shell built-in
covered in the bash
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
type function
.
We can see that it is a keyword.
And then now, let's view our script here, and we're using
nano
.
(Below is the screen from within nano
.)
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.)
(Hit Ctrl-x
to leave
nano
and
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 function
.
And you know the syntax for commands, options and arguments.
One last tip about the function
command
(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 and thorughout this website. Connect so you don't miss the next strings-free way to learn.
if
statement,
hit Back.while
keyword
helps us create a program loop, click Next./ factorpad.com / tech / linux-essentials / function-command.html
A newly-updated free resource. Connect and refer a friend today.