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 :: how to merge branch to master 
Shell :: aws cli has no installation package in ubuntu server 20.04 how to solve 
Shell :: installer lamp ubuntui 
Shell :: unix get epoch in miliseconds shell 
Shell :: powershell script enable tls 1.2 
Shell :: git aliases 
Shell :: list inactive services ubuntu 
Shell :: gh clone 
Shell :: distutils.sysconfig install 
Shell :: elk max virtual memory areas vm.max_map_count [65530] is too low 
Shell :: tmux kill all sessions 
Shell :: linux bash clean up log files in /var/log 
Shell :: download brave linux 
Shell :: linux rename 
Shell :: how to reverse shell 
Shell :: could not open lock file "/tmp/.s.PGSQL.5432.lock": Permission denied 
Shell :: anaconda install package 
Shell :: install polar linux 
Shell :: how to update composer in laravel 
Shell :: git remove staged area 
Shell :: linux terminal color change 
Shell :: what version of linux am i running 
Shell :: Error: Unable to find a match: ansible 
Shell :: clone with ssh gitlab fatal: Could not read from remote repository. 
Shell :: add images in readme github file 
Shell :: vim open new tabs 
Shell :: mkdir multiple directories windows 
Shell :: nvcc not working after installing cuda 
Shell :: npm vs yarn command 
Shell :: install node package manager 
ADD CONTENT
Topic
Content
Source link
Name
3+8 =