Sharing the data with many functions

We can create variables that may contain strings or numerical values. These global variables can be accessed by all the functions inside a script.

A simple script called function_11.sh with functions is as follows:

#!/bin/bash 
# We will define variable temp for sharing data with function 
temp="/temp/filename" 
 
remove_file() 
{ 
  echo "removing file $temp..." 
} 
remove_file 

Test the script as follows:

    $ chmod +x function_11.sh
    $ ./function_11.sh
  

This should produce the following output:

    removing file /temp/filename...
  

Get Learning Linux Shell Scripting - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.