Bash Parse Input Arguments And Functions With Parameters – Check Simple 2 Examples!

Share this post and Earn Free Points!

In this tutorial" I will show you how Bash parse input arguments and functions with parameters.

Introduction

In Bash, you can parse input arguments and define functions with parameters using the $1, $2, $3, etc. variables. These variables represent the arguments passed to the script or function, with $1 representing the first argument, $2 representing the second argument, and so on.

What is Bash?

Bash is basically the modern layer to use of sh. (Bash Arguments Parsing) As we can read on official page bash is described as: “It offers functional improvements over sh for both programming" and interactive use. In addition, most sh scripts can be run by Bash without modification.”

  • command-line editing,
  • unlimited size command history,
  • job control,
  • shell" functions and aliases,
  • indexed arrays of unlimited size,
  • integer arithmetic in any base from two to sixty-four.

Bash (short for “Bourne-Again SHell”) is a Unix shell" and command language. It is the default shell on many Linux and macOS" systems, and is also available on other platforms such as Windows".

A shell" is a command-line interface" that allows users to interact with the operating system by entering commands at a prompt. Bash is a popular shell" because it is powerful and has a large number of built-in commands, as well as the ability to run scripts written in the Bash programming language.

In addition to being a shell", Bash is also a programming language. It has features such as variables, loops, and conditional statements, which allow you to write scripts to automate tasks and perform more complex operations. Bash scripts are interpreted, which means they are not compiled like other programming languages. Instead, they are executed directly by the Bash interpreter.

Usage Of Bash

Bash is a Unix shell" and command-language interpreter that is widely used on Linux" and other Unix-like operating systems. It is the default shell" on many Linux distributions and is also available on macOS.

Bash is used in a variety of ways, including:

  • Interacting with the operating system: You can use Bash to issue commands to the operating system, navigate the file system, and manage files and directories.
  • Writing scripts to automate tasks: Bash includes a programming" language that allows you to write scripts to automate tasks such as backing up files, setting up a development environment, or deploying code.
  • Customizing the command-line environment: Bash provides a number of features that allow you to customize your command-line environment, such as aliases, functions, and environment variables".
  • Running command-line programs: Bash provides a convenient interface for running command-line programs, such as text editors, version control systems, and programming languages.

Bash is an important tool for developers and system administrators, and is widely used in the Linux community. It is a powerful and flexible tool that allows you to perform a wide variety of tasks on the command line".

Bash Parse input arguments (Bash Space-Separated)

We very often want to run a bash script with input arguments. The question often arises how to do it in an appropriate, clean and transparent way, the so-called production and which will be easy to change or develop in the future. And it will definitely happen 🙂

Let’s take the example of running a script and passing two parameters to it. Suppose we want to specify the name of the environment (-e) and the number of iterations (-i) of some code execution inside the script:

./test-script.sh -e DEV -i 10

Then inside the test-script.sh we can parse it using the following code:

usage() {
  echo "Usage: $0 [option...]" >&2
  echo "   -e     env"
  echo "   -i     iterations"
  exit 1
}

while getopts ":e:i:" o; do
  case "${o}" in
  e)
    ENV=${OPTARG}
    ;;
  i)
    ITERATIONS=${OPTARG}
    ;;
  *)
    usage
    ;;
  esac
done
shift $((OPTIND - 1))

Bash Function With Parameters

We very often want to create a function with parameters. But it is not as obvious as in other programming languages where parameters are passed in parentheses. We have to get to them covertly. The function call looks like:

someFunction "$ENV" "$ITERATIONS"

The function is implemented as follows:

someFunction() {

  environment=$1
  iterations=$2

  ...

}

For the next parameters, we refer to the $ and its number, starting from 1.

Bash Parse Input Arguments And Functions With Parameters

#!/bin/bash

# Print the first argument
echo "First argument: $1"

# Print the second argument
echo "Second argument: $2"

# Define a function with a parameter
function greet {
  # Print the greeting message
  echo "Hello, $1!"
}

# Call the function and pass an argument
greet "John"

To run this script, you can pass two arguments when you call it, like this:

./script.sh arg1 arg2

This will print the following output:

First argument: arg1
Second argument: arg2
Hello, John!

You can also use the shift command to shift the arguments to the left, which allows you to process the arguments in a loop. For example:

#!/bin/bash

# Print all arguments
while [[ $# -gt 0 ]]; do
  echo "Argument: $1"
  shift
done

This script will print each argument on a separate line.

Summary

That’s all about topic: Bash Parse input arguments and functions with parameters! I

In Bash, arguments are input values that are passed to a script or function when it is called. Arguments are specified after the script or function name, separated by spaces.

Could You Please Share This Post? 
I appreciate It And Thank YOU! :)
Have A Nice Day!

How useful was this post?

Click on a star to rate it!

Average rating 4.6 / 5. Vote count: 219

No votes so far! Be the first to rate this post.

As you found this post useful...

Follow us on social media!

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?