Recursive Function in Bash Scripting

LinuxLinuxIntermediate
Practice Now

Introduction

In this challenge, we will learn about recursive functions in bash scripting. We will create a function that will print each argument passed to the function using recursion. We will pass command-line arguments to the script, and the script will call the function with the same arguments. Regardless of how many arguments are passed, we will only echo the first positional argument using recursion.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL shell(("`Shell`")) -.-> shell/ControlFlowGroup(["`Control Flow`"]) linux(("`Linux`")) -.-> linux/BasicSystemCommandsGroup(["`Basic System Commands`"]) linux(("`Linux`")) -.-> linux/InputandOutputRedirectionGroup(["`Input and Output Redirection`"]) 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/exit("`Shell Exiting`") linux/BasicSystemCommandsGroup -.-> linux/echo("`Text Display`") linux/InputandOutputRedirectionGroup -.-> linux/redirect("`I/O Redirecting`") linux/BasicSystemCommandsGroup -.-> linux/bc("`Arithmetic Calculations`") shell/BasicSyntaxandStructureGroup -.-> shell/shebang("`Shebang`") shell/BasicSyntaxandStructureGroup -.-> shell/comments("`Comments`") shell/BasicSyntaxandStructureGroup -.-> shell/quoting("`Quoting Mechanisms`") shell/VariableHandlingGroup -.-> shell/variables_decl("`Variable Declaration`") shell/VariableHandlingGroup -.-> shell/str_manipulation("`String Manipulation`") shell/VariableHandlingGroup -.-> shell/arrays("`Arrays`") shell/VariableHandlingGroup -.-> shell/param_expansion("`Parameter Expansion`") shell/ControlFlowGroup -.-> shell/for_loops("`For Loops`") shell/ControlFlowGroup -.-> shell/cond_expr("`Conditional Expressions`") shell/ControlFlowGroup -.-> shell/exit_status("`Exit and Return Status`") shell/FunctionsandScopeGroup -.-> shell/func_def("`Function Definition`") shell/AdvancedScriptingConceptsGroup -.-> shell/arith_expansion("`Arithmetic Expansion`") shell/AdvancedScriptingConceptsGroup -.-> shell/subshells("`Subshells and Command Groups`") shell/SystemInteractionandConfigurationGroup -.-> shell/globbing_expansion("`Globbing and Pathname Expansion`") subgraph Lab Skills shell/if_else -.-> lab-18293{{"`Recursive Function in Bash Scripting`"}} linux/exit -.-> lab-18293{{"`Recursive Function in Bash Scripting`"}} linux/echo -.-> lab-18293{{"`Recursive Function in Bash Scripting`"}} linux/redirect -.-> lab-18293{{"`Recursive Function in Bash Scripting`"}} linux/bc -.-> lab-18293{{"`Recursive Function in Bash Scripting`"}} shell/shebang -.-> lab-18293{{"`Recursive Function in Bash Scripting`"}} shell/comments -.-> lab-18293{{"`Recursive Function in Bash Scripting`"}} shell/quoting -.-> lab-18293{{"`Recursive Function in Bash Scripting`"}} shell/variables_decl -.-> lab-18293{{"`Recursive Function in Bash Scripting`"}} shell/str_manipulation -.-> lab-18293{{"`Recursive Function in Bash Scripting`"}} shell/arrays -.-> lab-18293{{"`Recursive Function in Bash Scripting`"}} shell/param_expansion -.-> lab-18293{{"`Recursive Function in Bash Scripting`"}} shell/for_loops -.-> lab-18293{{"`Recursive Function in Bash Scripting`"}} shell/cond_expr -.-> lab-18293{{"`Recursive Function in Bash Scripting`"}} shell/exit_status -.-> lab-18293{{"`Recursive Function in Bash Scripting`"}} shell/func_def -.-> lab-18293{{"`Recursive Function in Bash Scripting`"}} shell/arith_expansion -.-> lab-18293{{"`Recursive Function in Bash Scripting`"}} shell/subshells -.-> lab-18293{{"`Recursive Function in Bash Scripting`"}} shell/globbing_expansion -.-> lab-18293{{"`Recursive Function in Bash Scripting`"}} end

Recursive Function

In this challenge, you are required to create a script that uses a recursive function to print each argument passed to the function.

Tasks

You need to complete the following task:

  • Create a script that calls a recursive function to print the first positional argument regardless of the number of arguments passed.

Requirements

You must meet the following requirements:

  • The script should be named recFunction.sh.
  • Pass command-line arguments to the script.
  • The script should call a function with the same arguments.
  • Regardless of how many arguments are passed, only the first positional argument should be echoed (echo $1).

Example

labex:project/ $ bash recFunction.sh 12 a bc 3
[OUTPUT] Arguments passed are [OUTPUT]
12
a
bc
3

Summary

After completing this challenge, we have learned how to create recursive functions in bash scripting. We have also learned how to pass command-line arguments to a script and call a function with the same arguments. We have used recursion to print each argument passed to the function. This challenge has helped us to improve our understanding of functions in bash scripting and how to use them effectively.

Other Linux Tutorials you may like