Intermediate Bash - Functions

Question Click to View Answer

Write a function called greeting that prints the string "hello world" to the Terminal.

# define the function
greeting () {
  echo "hello world"
}

# call the function
greeting

What does the following code print?

blah () { echo "Me so sleepy"; }
blah
echo $?

0

The blah function runs successfully and the exit status (stored in the $? variable) returns 0 to indicate success. If an exit status is not explicitly defined, the exit status is the exit statement of the last statement that ran in the function.

What does the following code return?

cats () {
  echo "Meow"
  return 42
}
cats
echo $?

42

The cats function uses the return keyword to return an exit status of 42. Notice that the return keyword cannot be used to return a value, it's just used to return an exit status. When the cats function is initially invoked, the "Meow" string is printed to the console, but that's not what is actually returned by the function. $? gives the exit status of the last command executed (the cats function in this example).

What's the problem with the following function definition?

toothbrush () { echo "clean me" }

The statements must end with a semicolon (;) for single-line function definitions. Here's the proper way to define the toothbrush function:

toothbrush () { echo "clean me"; }

What's the problem with the following function definition?

boring () { ; }

Functions cannot be empty. They must contain at least one statement.

What does the following code print to the Terminal? Explain the result.

add () {
  result=$(( $1 + $2 ))
}
add 3 4
echo $result

7

$1 and $2 are positional parameters that correspond to the arguments that are passed to the add function ($1 equals 3 and $2 equals 4). The add function creates a global variable, result, that can be accessed outside of the add function. Global variables create difficult bugs in large programs, so writing functions in this manner is not desirable.

What does the following code print to the Terminal? Explain the result.

sick_band () {
  local name="Beastie Boys"
}
sick_band
echo $name

echo $name doesn't return anything, even after the sick_band function is called. The local keyword is used when defining the name variable so name is only accessible within the sick_band function definition and is not accessible as a global variable. It's best to use local variables whenever possible and avoid global variables.

What does the following code print? Explain.

full_name () {
  echo "$1 $2"
}

name=$(full_name bob lob)
echo "My name is $name"

My name is bob lob

The full name function does not set a global variable or return a value. full_name echoes a string that can be captured in a variable and used throughout the program.

What does the following code print? The code is sourced from linuxjournal.com.

function myfunc()
{
  local  __resultvar=$1
  local  myresult='some value'
  eval $__resultvar="'$myresult'"
}

myfunc result
echo $result

some value

The result variable is passed to the myfunc argument as a parameter. The eval statement is used to generate the code result='some value', which sets result as a global variable that can be accessed outside the function definition. This method is convoluted, but odd solutions are required when the programming language does not do something basic like returning a value from a function.