Code Library
Home Submit Free Hosting Link To Us Contacts

Bash Passing an indirect reference to a function

Bash Passing an indirect reference to a function Bash Bash Passing an indirect reference to a function Download (.zip)



#!/bin/bash
# ind-func.sh: Passing an indirect reference to a function.

echo_var ()
{
echo "$1"
}

message=Hello
Hello=Goodbye

echo_var "$message" # Hello
# Now, let's pass an indirect reference to the function.
echo_var "${!message}" # Goodbye

echo "-------------"

# What happens if we change the contents of "hello" variable?
Hello="Hello, again!"
echo_var "$message" # Hello
echo_var "${!message}" # Hello, again!

exit 0






Bash Passing an indirect reference to a function