#!/usr/bin/env sh
foo 1 # this will fail because foo has not been declared yet.
foo() {
echo "Parameter #1 is $1"
}
foo 2 # this will work.
#!/bin/bash
#!/bin/bash
# Name - math.sh
# Purpose - Demo return value
# ------------------------------------
## user define function
math(){
local a=$1
local b=$2
local sum=$(( a + b))
return $sum
}
## call the math function with 5 and 10 as arguments
math 5 10
## display back result (returned value) using $?
echo "5 + 10 = $?"
Function shell variables
All function parameters or arguments can be accessed via $1, $2, $3,..., $N.
$0 always point to the shell script name.
$* or $@ holds all parameters or arguments passed to the function.
$# holds the number of positional parameters passed to the function.
How do I display function name?
$0 always point to the shell script name. However, you can use an array variable called FUNCNAME which contains the names of all shell functions currently in the execution call stack. The element with index 0 is the name any currently-executing shell function.This variable exists only when a shell function is executing.
FUNCNAME in action
Create a shell script called funcback.sh:
#!/bin/bash
# funcback.sh : Use $FUNCNAME
backup(){
local d="$1"
[[ -z $d ]] && { echo "${FUNCNAME}(): directory name not specified"; exit 1; }
echo "Starting backup..."
}
backup $1
#!/bin/bash
# Name - cmdargs.sh
# Purpose - Passing positional parameters to user defined function
# -----------------------------------------------------------------
file="$1"
# User-defined function
is_file_dir(){
# $f is local variable
local f="$1"
# file attributes comparisons using test i.e. [ ... ]
[ -f "$f" ] && { echo "$f is a regular file."; exit 0; }
[ -d "$f" ] && { echo "$f is a directory."; exit 0; }
[ -L "$f" ] && { echo "$f is a symbolic link."; exit 0; }
[ -x "$f" ] && { echo "$f is an executeble file."; exit 0; }
}
# make sure filename supplied as command line arg else die
[ $# -eq 0 ] && { echo "Usage: $0 filename"; exit 1; }
# invoke the is_file_dir and pass $file as arg
is_file_dir "$file"
#!/bin/bash
my_func() {
echo "$1 $2 $3"
}
my_func "hello" "grepper" "friends"
#!/bin/bash
# Name: bash-task.sh
# Purpose: Check if a file exists or not using custom bash function and return value
# -----------------------------------------------------------------------------------
# set values
readonly TRUE=0
readonly FALSE=1
# get input from the CLI
file="$1"
# return $TRUE (0) if file found
# return $FALSE (1) if file not found
is_file_found(){
[ -f "$1" ] && return $TRUE || return $FALSE
}
# show usage info if $1 not passed to our script
if [ $# -eq 0 ]
then
echo "Usage: $0 filename"
exit 1
fi
# let us call our function
is_file_found "$file"
# take action based upon return value
if [ $? -eq 0 ]
then
echo "$file added to backup task"
else
echo "$file not found."
fi
#!/bin/bash
# write a function
fresh(){
# t stores $1 argument passed to fresh()
t=$1
echo "fresh(): $0 is $0"
echo "fresh(): $1 is $1"
echo "fresh(): $t is $t"
echo "fresh(): total args passed to me $#"
echo "fresh(): all args ($@) passed to me -"$@""
echo "fresh(): all args ($*) passed to me -"$*""
}
# invoke the function with "Tomato" argument
echo "**** calling fresh() 1st time ****"
fresh Tomato
# invoke the function with total 3 arguments
echo "**** calling fresh() 2nd time ****"
fresh Tomato Onion Paneer
function_name(){
command_block_here
}
## OR ##
function function_name_here(){
command_line_block
}
## passing parameters to a Bash function ##
my_function_name(){
arg1=$1
arg2=$2
command on $arg1
}