Skip to content

Yet another programming solutions log

Sample bits from programming for the future generations.

Technologies Technologies
  • Algorithms and Data Structures
  • Java Tutorials
  • JUnit Tutorial
  • MongoDB Tutorial
  • Quartz Scheduler Tutorial
  • Spock Framework Tutorial
  • Spring Framework
  • Bash Tutorial
  • Clojure Tutorial
  • Design Patterns
  • Developer’s Tools
  • Productivity
  • About
Expand Search Form

Bash define function

farenda 2015-05-25 0

Problem:

Bash allows to define functions, which is very good way to reuse parts of shell scripts. This tutorial shows how to define Bash functions and apply in scripts.

Solution:

Function can be defined using the function keyword, like so:

#!/bin/bash

# $0 is a Bash variable that resolves to script's name
function print_usage {
    echo "Usage: $0 [message]"
    echo "Prints message and exits."
    # the following line ends the script
    exit 1
}

# function parameters are accessible as variables $1, $2, ...
function my_echo {
    echo "Number of params passed to the my_echo function: $#"
    echo "${1}"
}

echo "Number of params passed to the script: $#"

if [ $# -eq 0 ]; then
    # function without arguments can be called like that:
    print_usage
else
    # all parameters passed to the script are joined
    # and passed as one to the function:
    my_echo "$*"
fi

Calling the script without parameters:

$> ./echo.sh
Number of params passed to the script: 0
Usage: ./echo.sh [message]
Prints message and exits.

Calling the script with one parameter:

$> ./echo.sh Hello
Number of params passed to the script: 1
Number of params passed to the my_echo function: 1
Hello

Calling the script with more than one parameter (note the number of parameters!):

$> ./echo.sh Hello World
Number of params passed to the script: 2
Number of params passed to the my_echo function: 1
Hello World

Remember to define function before it’s use. Otherwise you will receive the following error:

$> ./echo.sh
./echo.sh: line 4: print_usage: command not found

Defining Bash functions is basic of advanced shell scripting. From now on possibilities are endless! :-)

Share with the World!
Categories Bash Tags bash
Previous: Java 8 write to a text file
Next: Fizz Buzz in Java

Recent Posts

  • Java 8 Date Time concepts
  • Maven dependency to local JAR
  • Caesar cipher in Java
  • Java casting trick
  • Java 8 flatMap practical example
  • Linked List – remove element
  • Linked List – insert element at position
  • Linked List add element at the end
  • Create Java Streams
  • Floyd Cycle detection in Java

Pages

  • About Farenda
  • Algorithms and Data Structures
  • Bash Tutorial
  • Bean Validation Tutorial
  • Clojure Tutorial
  • Design Patterns
  • Java 8 Streams and Lambda Expressions Tutorial
  • Java Basics Tutorial
  • Java Collections Tutorial
  • Java Concurrency Tutorial
  • Java IO Tutorial
  • Java Tutorials
  • Java Util Tutorial
  • Java XML Tutorial
  • JUnit Tutorial
  • MongoDB Tutorial
  • Quartz Scheduler Tutorial
  • Software Developer’s Tools
  • Spock Framework Tutorial
  • Spring Framework

Tags

algorithms bash bean-validation books clojure design-patterns embedmongo exercises git gof gradle groovy hateoas hsqldb i18n java java-basics java-collections java-concurrency java-io java-lang java-time java-util java-xml java8 java8-files junit linux lists log4j logging maven mongodb performance quartz refactoring regex rest slf4j solid spring spring-boot spring-core sql unit-tests

Yet another programming solutions log © 2021

sponsored
We use cookies to ensure that we give you the best experience on our website. If you continue to use this site we will assume that you are happy with it.Ok