FactorPad
Build a Better Process

Linux bash Shell Script function with Examples

Linux function allows you to create a script within a script (a function is technically a bash keyword, not a command).
  1. Purpose - Learn what function is for and how to find help.
  2. Options - Review a few common options and arguments.
  3. Examples - Walk through code examples with function.
  4. Script - Add function to our script and run it.
  5. A tip - Finish off with one more insight.
face pic by Paul Alan Davis, CFA
Updated: February 24, 2021
In this tutorial on Linux function, 95 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 Tip Next

/ factorpad.com / tech / linux-essentials / function-command.html


An ad-free and cookie-free website.


Examples of the Linux function Command (Keyword)

Intermediate

Learn to create a script within a script at the Linux command line.

Video Tutorial

Videos can also be accessed from the Linux Essentials Playlist on YouTube.

Linux bash Shell Script function with Examples (3:43)

Video Script

The Command and Why You Need It

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.

Common Linux function Options
-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.

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. To confirm, let's quickly use type function.

$ type function function is a shell keyword

We can see that it is a keyword.

And then now, let's view our script here, and we're using nano.

$ nano bin/funscript

(Below is the screen from within nano.)

GNU nano 2.2.6 File: /home/factorpad/bin/funscript #!/bin/bash # The shebang gives the absolute path to the executable program # To see if it points to the right executable try: which bash # This is our first script called funscript # From video 95, we add four functions above other code # First form uses: functions { commands return } syntax # 1) Function prints system statistics function fun_stats { printf "\tCollecting system status\n" echo "Disk space and % used" df -h --output='size','pcent' /home sleep 5s return } # Second form uses: name () { command return } syntax # 2) Function collects returns text file data from factorpad.com website fun_returns () { printf "\tCollecting returns data\n" wget factorpad.com/returns.txt --quiet -O video95.txt printf "\tInvestment modeling boot camp data downloaded\n" sleep 5s return } (the rest of the lines in script trimmed) ^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

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.

A Final Tip

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).


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 and thorughout this website. Connect so you don't miss the next strings-free way to learn.

Outline Back Tip Next

/ factorpad.com / tech / linux-essentials / function-command.html


linux function
bash function
shell script
shell function
shell script function
bash script
bash script function
shell script example
linux shell script
function in shell script
function bash
bash call function
bash shell script
bash if then
linux programming
bash function example
shell script variable
shell scripting
shell programming
bash function return
bash script function
bash function examples
bash scripting tutorial
linux programming
bash scripting example

A newly-updated free resource. Connect and refer a friend today.