JIYIK CN >

Current Location:Home > Learning > OPERATING SYSTEM >

Getting optional input arguments in Bash

Author:JIYIK Last Updated:2025/03/21 Views:

Sometimes we need to create a dynamic function which can be executed in both without passing any arguments or without passing any arguments. For this we need to set some default values ​​for those arguments so that if the arguments are not passed while calling the function, it can use the default arguments.

This article will demonstrate how to create optional input parameters for functions. We will also look at some examples and explanations to make the topic easier to understand.


Getting optional input arguments in Bash

In the following example, we are printing the default values. We have declared 4 variables which contain the default values.

The code for our example is as follows:

VAR1=${1:-foo}
VAR2=${2:-bar}
VAR3=${3:-1}
VAR4=${4:-$(date)}

echo "$VAR1"
echo "$VAR2"
echo "$VAR3"
echo "$VAR4"

After executing the code, we will get the following output:

foo
bar
1
Sat Aug 13 14:59:31 +06 2022

Now let's look at an advanced example. In the example shared below, we have created a function that can take 0, 1, and 2 parameters and set the default value inside the function.

Now, the code for our example looks like this:

FindSum()
{
    VAR1=${1:-40}
    VAR2=${2:-50}

    if [ $# -lt 1 ];
    then
        echo "The provided parameters are: $VAR1, $VAR2."
    elif [ $# -lt 2 ];
    then
        echo
        echo "The provided parameters is: $VAR2."
    else
        echo
        echo "No parameters provided"
        VAR1=$1
        VAR2=$2
    fi
    SUM=$((VAR1+VAR2))
    echo "The sum of $VAR1 and $VAR2 is $SUM"
}

FindSum
FindSum 40
FindSum 70 30

Let's explain the code piece by piece.

First, we set the default values ​​of the variables using the lines VAR1=${1:-40}and VAR2=${2:-50}. Afterwards, we check if values ​​for these variables were given.

If the value does not provide them, we have to use its default value; otherwise, use the value given by the user.

Finally, we have just SUM=$((VAR1+VAR2))performed a sum operation using the row and printed the result. Here, we have called the function with FindSum without any parameters.

Similarly, we call a function with one parameter using FindSum line 40. We call a function with two parameters using FindSum line 70 30.

The provided parameters are: 40, 50.
The sum of 40 and 50 is 90

The provided parameters is: 50.
The sum of 40 and 50 is 90

No parameters provided
The sum of 70 and 30 is 100

All the codes used in this article are written in Bash. It will only work in Linux Shell environment.

For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.

Article URL:

Related Articles

Check if input parameter exists in Bash

Publish Date:2025/03/21 Views:191 Category:OPERATING SYSTEM

When we create a Bash script, we may want to use parameters in our script to run successfully. Therefore, we need to create a script to check the number of input parameters used by the user in the script. All of this prevents unexpected beh

Using Double and Single Pipes in Bash

Publish Date:2025/03/21 Views:136 Category:OPERATING SYSTEM

In Bash, the double pipe || is also known as the OR operator in other programming languages. On the other hand, the single pipe | is known as a pipeline. In this article, we will see how to use double pipe (also known as OR) and pipelines i

Breaking out of a loop in Bash

Publish Date:2025/03/21 Views:131 Category:OPERATING SYSTEM

Using loops is a common task in any programming or scripting language. While using a loop, sometimes we need to stop it under a predefined condition. Like other programming and scripting languages, Bash uses the keyword break to stop any lo

Bash configuration files in macOS

Publish Date:2025/03/21 Views:185 Category:OPERATING SYSTEM

In this article, we will discuss how to create, delete, and edit bash profiles in macOS. Create .bash_profile To create .bash_profile, open a terminal and move to your home directory using the following command. cd ~/ Now you can create usi

eval command in bash script

Publish Date:2025/03/21 Views:87 Category:OPERATING SYSTEM

This article is about using strings as commands in Bash scripts. For this purpose, the eval command is used. Eval Command in Bash Scripts In some Bash scripts, you have to create a string using variables or input values ​​(for example)

Exit Bash Script

Publish Date:2025/03/21 Views:99 Category:OPERATING SYSTEM

This article provides a brief introduction to Bash scripting and discusses exiting a Bash script when an error occurs. It further discusses the limitations and benefits of Bash scripting. What is Bash Scripting A computer script/program tel

timeout command in Bash

Publish Date:2025/03/21 Views:105 Category:OPERATING SYSTEM

This article is a simple guide to setting a timeout for a specific program using the timeout command from GNU's coreutils package in Bash. timeout command in Bash In many cases, such as fetching data from a server or running a function or p

Start a new terminal session in Bash

Publish Date:2025/03/21 Views:51 Category:OPERATING SYSTEM

In various situations when using Bash or another shell, you may need to run a script or program in a new terminal instance or in another tab in the same terminal. Opening a new terminal instance or tab from within Terminal is simple; we'll

Checking Syntax in Bash

Publish Date:2025/03/21 Views:89 Category:OPERATING SYSTEM

This article discusses methods for checking Bash scripts for syntax problems without running the script. Syntax errors are caused by some grammatical or spelling mistakes in the code. In modern compilers of other programming languages, the

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Recommended

Tags

Scan the Code
Easier Access Tutorial