Bash – Function – Return String from a function

Background

Playing around with Bash and just really wrestling on how to tokenize a string and return its parts.

Honestly, it is much simpler than that.

It can only have two whole pieces; I cheated and made sure of that while curating the dataset.

 

Options

Here are the options available for returning data from a function

  1. Use echo <data> to return the intended data
  2. Use name reference

 

Echo Data

Outline

  1. Calling Function
    • Invoke child function
      • Syntax
        • variable=$(function)
      •  Sample
        • usernameRet_=$(getUserInput)
  2. Called Function
    • Return value using echo
      • Syntax
        •  echo <variable> 
      • Sample
        •  echo "$username_" 

Code


#!/bin/bash

function getUserInput()
{
	#declare variables	
	local username_=""

	#read data
	read username_

	#return variables
	echo "$username_"
    
}

#greetings
echo "hello, who am I talking to..."

#get data
usernameRet_=$(getUserInput)

echo "The name entered is $usernameRet_"

 

Pass by Name Reference

Outline

  1. Calling Function
    • Invoke child function
      • Pass along reference
        • Notice that variable is noted without $
  2. Called Function
    • Declare Variable and Set equal to the positioned argument
      • Syntax
        •  declare -n [variable]=${n} 
      • Sample
        •  declare -n usernameref_=$1 
    • In function, set a reference variable to the new value
      • Syntax
        •  [variable]=[value] 
      • Sample
        •  usernameref_="samuel" 

Code


#!/bin/bash

function getUserInput()
{

    #declare a name reference
    #The name reference should reference the first parameter
    declare -n usernameref_=$1

    #prompt user for his/her name
    echo Hello, who am I talking to?
    
    #accept name and place in name reference
    read usernameref_
    
}

# Hello World Program in Bash Shell
username=''

#invoke function
#pass variable by reference
getUserInput username

#echo data
echo "Hello $username"

 

Source Code Control

GitHub

Gist

Link

#!/bin/bash
function getUserInput()
{
#declare variables
local username_=""
#read data
read username_
#return variables
echo "$username_"
}
#greetings
echo "hello, who am I talking to…"
#get data
usernameRet_=$(getUserInput)
echo "The name entered is $usernameRet_"

#!/bin/bash
function getUserInput()
{
#declare a name reference
#The name reference should reference the first parameter
declare -n usernameref_=$1
#prompt user for his/her name
echo Hello, who am I talking to?
#accept name and place in name reference
read usernameref_
}
# Hello World Program in Bash Shell
username=''
getUserInput username
echo "Hello $username"

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s