shell script that takes optional arguments

Bash Script That Takes Optional Arguments

Shell Scripts allow you to automate tasks and processes in Linux. Many of them accept user arguments to customize the result. But sometimes you may need a shell script that accepts optional arguments. In this article, we will learn how to write bash script that takes optional arguments.

Bash Script That Takes Optional Arguments

You can easily setup optional arguments for shell script using its default-value syntax.

command ${1:-foo}

In the above example, we mention argument number 1,2,3,.. followed by colon, followed by hyphen and the default value. In this case, if the parameter (e.g. $1) is null or unset then default value (e.g. foo) is used, else parameter is substituted.

If you only want to substitute the value if the variable is unset, and not null, then use the following syntax.

command ${1-foo}

Basically, if you include colon in the above expression, it will test the parameter for its existence and non null value.

Here is a simple example of a shell script that accepts 4 optional arguments.

#!/usr/bin/env bash


echo "$ARG1"
echo "$ARG2"
echo "$ARG3"
echo "$ARG4"

Here are some examples to run this script with optional arguments.

$ ./
Thu Mar 29 10:03:20 ADT 2022

$ ./ ez
Thu Mar 29 10:03:40 ADT 2022

$ ./ able was i
Thu Mar 29 10:03:54 ADT 2022

$ ./ "able was i"
able was i
Thu Mar 29 10:04:01 ADT 2022

$ ./ "able was i" super
able was i
Thu Mar 29 10:04:10 ADT 2022

$ ./ "" "super duper"
super duper
Thu Mar 29 10:05:04 ADT 2022

$ ./ "" "super duper" hi you
super duper

Let us look at each of the examples one by one.

In the first example, we don’t pass any input argument, so our script echoes the default value of all optional arguments.

In the second example, we pass default value of only the first argument, so our script displays the supplied value for first argument, and default values of other arguments.

In the third example, we pass default values for first 3 arguments, so the script displays those values and the default value for 4th argument.

In the fourth example, we pass the same input as used in 3rd example, but we enclose it in quotes, so it is treated as a single string as used a default value of first argument. For rest of the variables, default values are displayed.

In the 5th example, we use double quotes to enclose 3 words, and separately pass values for second argument. Rest of the values are displayed with default values.

In the next example, we pass empty string as first argument, but you will see that the script displays the default value and disregards it. This is because we have mentioned colon(:) in our expression.

In the last example, we pass empty string as first argument, ‘super duper’ within quotes as second argument, and then a couple of space separated words as 3rd and 4th arguments. In this case, the script displays default value for first argument, and user supplied values for other arguments.

In this article, we have learnt how to setup shell scripts to accept optional arguments. As you can see there are different ways to define optional arguments, so you can customize them as per your requirement.

Also read:

How to Test If Variable is Number in Shell Script
How to Check if Variable is Number in Python
How to Take Backup of MySQL Database in Python
How to Prevent Accidental File Deletion in Linux
How to Make File & Directory Undeletable in Linux

Leave a Reply

Your email address will not be published. Required fields are marked *