Search
 
SCRIPT & CODE EXAMPLE
 

SHELL

bash function arguments

#!/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.
Comment

bash function arguments

#!/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 = $?"
Comment

bash function arguments

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
Comment

bash function arguments

#!/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"
Comment

bash function parameter

#!/bin/bash

my_func() {
    echo "$1 $2 $3"
}

my_func "hello" "grepper" "friends"
Comment

bash function arguments

#!/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
Comment

bash function arguments

#!/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
Comment

bash function arguments

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
}
Comment

PREVIOUS NEXT
Code Example
Shell :: Install command-line ipfs 
Shell :: how to edit and save crontab in linux 
Shell :: git delete branches not on remote 
Shell :: cs50 cli50 install 
Shell :: remove packages.sury.org in debian 
Shell :: Docker show currently running containers 
Shell :: command to restart a system service 
Shell :: aws cli cloudformation list stacks 
Shell :: git pull 
Shell :: if git status 
Shell :: install gdal django rest 
Shell :: Git - show all commits 
Shell :: remove a file from git commit history 
Shell :: shutdown command linux 
Shell :: install arch package 
Shell :: read input from stdin bash script 
Shell :: generate zip file terminal 
Shell :: how to create a shortcut in ubuntu 
Shell :: bash list files for user 
Shell :: brew Cannot install on Intel processor in ARM default prefix (/opt/homebrew)! 
Shell :: install redux 
Shell :: how to remove file from directory in linux 
Shell :: git rename other branch 
Shell :: hyper terminal 
Shell :: save command output to file in powershell 
Shell :: authentication failed for git 
Shell :: bash print file permissions 
Shell :: linux how to execute a script 
Shell :: scp all files in currrent directory 
Shell :: How to commit the code to the github 
ADD CONTENT
Topic
Content
Source link
Name
5+8 =