Passing String Value in Bash

LinuxLinuxBeginner
Practice Now

Introduction

Bash is a popular shell used in Linux and Unix operating systems. It provides a way to create functions that can pass both numeric and string values.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("`Linux`")) -.-> linux/BasicSystemCommandsGroup(["`Basic System Commands`"]) 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`"]) linux/BasicSystemCommandsGroup -.-> linux/echo("`Text Display`") linux/BasicSystemCommandsGroup -.-> linux/read("`Input Reading`") 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/variables_usage("`Variable Usage`") shell/FunctionsandScopeGroup -.-> shell/func_def("`Function Definition`") shell/AdvancedScriptingConceptsGroup -.-> shell/read_input("`Reading Input`") shell/AdvancedScriptingConceptsGroup -.-> shell/cmd_substitution("`Command Substitution`") shell/AdvancedScriptingConceptsGroup -.-> shell/subshells("`Subshells and Command Groups`") subgraph Lab Skills linux/echo -.-> lab-387370{{"`Passing String Value in Bash`"}} linux/read -.-> lab-387370{{"`Passing String Value in Bash`"}} shell/shebang -.-> lab-387370{{"`Passing String Value in Bash`"}} shell/comments -.-> lab-387370{{"`Passing String Value in Bash`"}} shell/quoting -.-> lab-387370{{"`Passing String Value in Bash`"}} shell/variables_decl -.-> lab-387370{{"`Passing String Value in Bash`"}} shell/variables_usage -.-> lab-387370{{"`Passing String Value in Bash`"}} shell/func_def -.-> lab-387370{{"`Passing String Value in Bash`"}} shell/read_input -.-> lab-387370{{"`Passing String Value in Bash`"}} shell/cmd_substitution -.-> lab-387370{{"`Passing String Value in Bash`"}} shell/subshells -.-> lab-387370{{"`Passing String Value in Bash`"}} end

Passing String Value in Bash

In this challenge, we will focus on passing a string value from a Bash function. We will create a function that returns a string value and then use that value in our script.

Requirements

  • The Bash script should be named ~/project/script.sh.
  • The script should use a function to return a string value.

Example

To run the script, use the following command:

./script.sh

Output:

Enter your name
John
Return value of the function is Hello, John

Summary

In this challenge, we learned how to pass a string value from a Bash function. We created a function that returns a string value and then used that value in our script. By using functions in Bash, we can create reusable code that can be used in multiple parts of our script.

Other Linux Tutorials you may like