Using a function with parameters within a for loop

In this short example, we have a function called create_file, which is called within a loop for each file in the FILES array. The function creates a file, modifies its permissions, and then passively checks for its existence using the ls command:

#!/bin/bashFILES=( "file1" "file2" "file3" ) # This is a global variablefunction create_file() {    local FNAME="${1}" # First parameter    local PERMISSIONS="${2}" # Second parameter    touch "${FNAME}"    chmod "${PERMISSIONS}" "${FNAME}"    ls -l "${FNAME}"}for ELEMENT in ${FILES[@]}do        create_file "${ELEMENT}" "a+x"doneecho "Created all the files with a function!"exit 0

Get Bash Cookbook 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.