Pass arguments into a function
From Linux Shell Scripting Tutorial - A Beginner's handbook
- Shell functions have their own command line argument.
- Use variable $1, $2..$n to access argument passed to the function.
- The syntax is as follows:
name(){
arg1=$1
arg2=$2
command on $arg1
}
- To invoke the the function use the following syntax:
name foo bar
Where,
- name = function name.
- foo = Argument # 1 passed to the function (positional parameter # 1).
- bar = Argument # 2 passed to the function.
Contents |
Example
Create a function called fresh.sh:
#!/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
Save and close the file. Run it as follows:
chmod +x fresh.sh ./fresh.sh
Sample outputs:
**** calling fresh() 1st time **** fresh(): $0 is ./fresh.sh fresh(): $1 is Tomato fresh(): $t is Tomato fresh(): total args passed to me 1 fresh(): all args ($@) passed to me -"Tomato" fresh(): all args ($*) passed to me -"Tomato" **** calling fresh() 2nd time **** fresh(): $0 is ./fresh.sh fresh(): $1 is Tomato fresh(): $t is Tomato fresh(): total args passed to me 3 fresh(): all args ($@) passed to me -"Tomato Onion Paneer" fresh(): all args ($*) passed to me -"Tomato Onion Paneer"
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
Save and close the file. Run it as follows:
chmod +x funcback.sh funcback.sh /home funcback.sh
Sample outputs:
backup(): directory name not specified
Example
Create a shell script to determine if given name is file or directory (cmdargs.sh):
#!/bin/bash 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"
Run it as follows:
./cmdargs.sh /etc/resolv.conf ./cmdargs.sh /bin/date ./cmdargs.sh $HOME ./cmdargs.sh /sbin
Sample outputs:
/etc/resolv.conf is a regular file. /bin/date is a regular file. /home/vivek is a directory. /sbin is a directory.