Shell Functions

ShellShellBeginner
Practice Now

This tutorial is from open-source community. Access the source code

Introduction

In this lab, we will learn about functions in shell programming. Functions are subroutines that implement a set of commands and operations. They are useful for performing repeated tasks.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL shell(("`Shell`")) -.-> shell/ControlFlowGroup(["`Control Flow`"]) linux(("`Linux`")) -.-> linux/BasicSystemCommandsGroup(["`Basic System Commands`"]) linux(("`Linux`")) -.-> linux/FileandDirectoryManagementGroup(["`File and Directory Management`"]) linux(("`Linux`")) -.-> linux/BasicFileOperationsGroup(["`Basic File Operations`"]) shell(("`Shell`")) -.-> shell/BasicSyntaxandStructureGroup(["`Basic Syntax and Structure`"]) shell(("`Shell`")) -.-> shell/VariableHandlingGroup(["`Variable Handling`"]) shell(("`Shell`")) -.-> shell/FunctionsandScopeGroup(["`Functions and Scope`"]) shell(("`Shell`")) -.-> shell/AdvancedScriptingConceptsGroup(["`Advanced Scripting Concepts`"]) shell(("`Shell`")) -.-> shell/SystemInteractionandConfigurationGroup(["`System Interaction and Configuration`"]) shell/ControlFlowGroup -.-> shell/if_else("`If-Else Statements`") linux/BasicSystemCommandsGroup -.-> linux/echo("`Text Display`") linux/FileandDirectoryManagementGroup -.-> linux/cd("`Directory Changing`") linux/BasicFileOperationsGroup -.-> linux/chmod("`Permission Modifying`") shell/BasicSyntaxandStructureGroup -.-> shell/comments("`Comments`") shell/BasicSyntaxandStructureGroup -.-> shell/quoting("`Quoting Mechanisms`") shell/VariableHandlingGroup -.-> shell/variables_decl("`Variable Declaration`") shell/VariableHandlingGroup -.-> shell/variables_usage("`Variable Usage`") shell/ControlFlowGroup -.-> shell/cond_expr("`Conditional Expressions`") shell/FunctionsandScopeGroup -.-> shell/func_def("`Function Definition`") shell/AdvancedScriptingConceptsGroup -.-> shell/arith_expansion("`Arithmetic Expansion`") shell/AdvancedScriptingConceptsGroup -.-> shell/cmd_substitution("`Command Substitution`") shell/AdvancedScriptingConceptsGroup -.-> shell/subshells("`Subshells and Command Groups`") shell/SystemInteractionandConfigurationGroup -.-> shell/globbing_expansion("`Globbing and Pathname Expansion`") linux/FileandDirectoryManagementGroup -.-> linux/wildcard("`Wildcard Character`") subgraph Lab Skills shell/if_else -.-> lab-153902{{"`Shell Functions`"}} linux/echo -.-> lab-153902{{"`Shell Functions`"}} linux/cd -.-> lab-153902{{"`Shell Functions`"}} linux/chmod -.-> lab-153902{{"`Shell Functions`"}} shell/comments -.-> lab-153902{{"`Shell Functions`"}} shell/quoting -.-> lab-153902{{"`Shell Functions`"}} shell/variables_decl -.-> lab-153902{{"`Shell Functions`"}} shell/variables_usage -.-> lab-153902{{"`Shell Functions`"}} shell/cond_expr -.-> lab-153902{{"`Shell Functions`"}} shell/func_def -.-> lab-153902{{"`Shell Functions`"}} shell/arith_expansion -.-> lab-153902{{"`Shell Functions`"}} shell/cmd_substitution -.-> lab-153902{{"`Shell Functions`"}} shell/subshells -.-> lab-153902{{"`Shell Functions`"}} shell/globbing_expansion -.-> lab-153902{{"`Shell Functions`"}} linux/wildcard -.-> lab-153902{{"`Shell Functions`"}} end

Create a Function

To create a function, use the function keyword followed by the function name and curly brackets {}. Inside the curly brackets, write the commands and operations that the function should perform.

function function_name {
  command...
}

Call a Function

Functions can be called simply by writing their names. A function call is equivalent to a command. You can also pass parameters to functions by specifying them after the function name. The first parameter is referred to in the function as $1, the second as $2, and so on.

function function_B {
  echo "Function B."
}
function function_A {
  echo "$1"
}
function adder {
  echo "$(($1 + $2))"
}

## FUNCTION CALLS
## Pass parameter to function A
function_A "Function A." ## Function A.
function_B               ## Function B.
## Pass two parameters to function adder
adder 12 56 ## 68

Create a file called ~/project/functions.sh and add the above code.

cd ~/project
chmod +x functions.sh
./functions.sh
Function A.
Function B.
68

Exercise - Create ENGLISH_CALC Function

In this exercise, we will create a function called ENGLISH_CALC that can process sentences like '3 plus 5', '5 minus 1', or '4 times 6', and print the results as '3 + 5 = 8', '5 - 1 = 4', or '4 * 6 = 24' respectively.

function ENGLISH_CALC {
  a=$1
  b=$3
  signn=$2
  if [ $signn == "plus" ]; then
    echo "$a + $b = $(($a + $b))"
  elif [ $signn == "minus" ]; then
    echo "$a - $b = $(($a - $b))"
  elif [ $signn == "times" ]; then
    echo "$a * $b = $(($a * $b))"
  fi
}

## Testing code
ENGLISH_CALC 3 plus 5
ENGLISH_CALC 5 minus 1
ENGLISH_CALC 4 times 6

Revise the file ~/project/functions.sh to include the above code.

cd ~/project
chmod +x functions.sh
./functions.sh
3 + 5 = 8
5 - 1 = 4
4 * 6 = 24

Summary

Congratulations! In this lab, you have learned how to create and call functions in shell programming. Functions are useful for organizing code and performing repetitive tasks. You can now use functions to simplify your shell scripts and make them more efficient.

Other Shell Tutorials you may like