Ana Sayfa / Linux / Bash Script / Bash Script Functions

Bash Script Functions

Functions with arguments



greet() {
 local name="$1"
 echo "Hello, $name"

greet "John Doe"

# running above script
$ bash helloJohn.
Hello, John Doe
  1. If you don’t modify the argument in any way, there is no need to copy it to a local variable – simply echo
"Hello, $1".

2.You can use $1, $2, $3 and so on to access the arguments inside the function.

Note: for arguments more than 9 $10 won’t work (bash will read it as $10), you need to do ${10}, ${11} and so on.

3.$@ refers to all arguments of a function:

foo() {
 echo "$@"

foo 1 2 3 # output => 1 2 3

Note: You should practically always use double quotes around “$@”, like here.

Omitting the quotes will cause the shell to expand wildcards (even when the user specifically quoted them in order to avoid that) and generally introduce unwelcome behavior and potentially even security problems.

foo "string with spaces;" '$HOME' "*"
# output => string with spaces; $HOME *

4. for default arguments use ${1:-default_val}. Eg:

foo() {
 local val=${1:-25}
 echo "$val"
foo # output => 25
foo 30 # output => 30

5. to require an argument use ${var:?error message}

foo() {
 local val=${1:?Must provide an argument}
 echo "$val"

Simple Function


# Define a function greet

greet ()
 echo "Hello World!"

# Call the function greet

In running the script, we see our message

$ bash
Hello World!

Note that sourcing a file with functions makes them available in your current bash session.

$ source # or, more portably, "."
$ greet
Hello World!

You can export a function in some shells, so that it is exposed to child processes.

bash -c 'greet' # fails
export -f greet # export function; note -f
bash -c 'greet' # success

Handling flags and optional parameters

The getopts builtin can be used inside functions to write functions that accommodate flags and optional parameters. This presents no special difficulty but one has to handle appropriately the values ouched by getopts. As an example, we define a failwith function that writes a message on stderr and exits with code 1 or an arbitrary code supplied as parameter to the -x option:

# Fail with the given diagnostic message
# The -x flag can be used to convey a custom exit status, instead of
# the value 1. A newline is automatically added to the output




 while getopts 'x:' OPTION; do

     case ${OPTION} in

        x) status="${OPTARG}";;
        *) 1>&2 printf 'failwith: %s: Unsupported option.\n' ${OPTION}";;


 shift $(( OPTIND - 1 ))
    printf 'Failure: '
    printf "$@"
    printf '\n'
 } 1>&2
 exit "${status}"

This function can be used as follows:

failwith '%s: File not found.' "${filename}"
failwith -x 70 'General internal error.'

and so on.

Note that as for printf, variables should not be used as first argument. If the message to print consists of the content of a variable, one should use the %s specifier to print it, like in

failwith '%s' "${message}"

Print the function definition

getfunc() {
 declare -f "$@"
function func(){
 echo "I am a sample function"
funcd="$(getfunc func)"
getfunc func # or echo "$funcd"


func ()
 echo "I am a sample function"

A function that accepts named parameters

foo() {
 while [[ "$#" -gt 0 ]]

    case $1 in

    local FOLLOW="following"
    local TAIL="tail=$2"


 echo "TAIL: $TAIL"

Example usage:

foo -f
foo -t 10
foo -f –tail 10
foo –follow –tail 10

Return value from a function

The return statement in Bash doesn’t return a value like C-functions, instead it exits the function with a return status. You can think of it as the exit status of that function.

If you want to return a value from the function then send the value to stdout like this:

fun() {
 local var="Sample value to be returned"
 echo "$var"
 #printf "%s\n" "$var"

Now, if you do:


the output of fun will be stored in $var.

The exit code of a function is the exit code of its last command

Consider this example function to check if a host is up:

is_alive() {
 ping -c1 "$1" &> /dev/null

This function sends a single ping to the host specified by the first function parameter. The output and error output of ping are both redirected to /dev/null, so the function will never output anything. But the ping command will have exit code 0 on success, and non-zero on failure. As this is the last (and in this example, the only) command of
the function, the exit code of ping will be reused for the exit code of the function itself.

This fact is very useful in conditional statements.

For example, if host graucho is up, then connect to it with ssh:

if is_alive graucho; then
 ssh graucho

Another example: repeatedly check until host graucho is up, and then connect to it with ssh:

while ! is_alive graucho; do
 sleep 5

ssh graucho

Bunada Göz Atın

PHP Types

Type Comparison There are two types of comparison: loose comparison with == and strict comparison …

Bir yanıt yazın

E-posta adresiniz yayınlanmayacak. Gerekli alanlar * ile işaretlenmişlerdir