Check if input parameter exists in Bash
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 behavior when a user does not pass the required parameters when using a script or command, and we can then pass an error message telling the user that they did not use the required number of parameters.
This article will show you how to check if an input parameter exists or the number of existing parameters.
Using $# in Bash to check if an input parameter exists
In Bash, a special variable $# holds the input parameters. Using $#, you can check how many input parameters have been passed to a Bash script.
A simple Bash script will show you what this $# variable means when passed with no arguments or when passed with two arguments.
#!/bin/bash
echo "The number of input arguments passed to this script: "
echo $#
Let's run the script without any input arguments:
$ ./script.sh
The terminal output is as follows:
The number of input arguments passed to this script:
0
Now, let's pass two arguments to the same script:
$ ./script.sh one two
The output of the script is as follows:
The number of input arguments passed to this script:
2
Now, we can use $# in our script with a conditional statement to check if $# is equal to zero (meaning no input parameters) to exit if true. If $#
$# is greater than 0, the condition becomes false and the else part of the conditional statement is executed.
#!/bin/bash
if [ $# -eq 0 ]
then
echo "No input arguments exist"
exit 1
else
echo "The number of input arguments passed:"
echo $#
fi
Let's run the script using the following command without arguments:
$ ./script.sh
Output of the code:
No input arguments exist
Now, let's run a different script command with arguments:
$ ./script.sh one two
The output of the code is different because the conditional check is equal to false:
The number of input arguments passed:
2
Apart from this, we can $[number]
access the input arguments using another special variable using . These are the positional parameters that we can use in Bash.
If we know we will get three variables or have already determined, we can access these three variables using the following code.
#!/bin/bash
echo "The input arguments are:"
echo $1 $2 $3
When three arguments are passed, the output of the code will be as follows:
The input arguments are:
one two three
Using $1 to check if an input parameter exists in Bash
Remember the positional parameters we discussed in the previous section. We can use the first $1 to check if any input parameters were passed, because if there were no input parameters, there $1
would be no value in the positional parameters.
Therefore, we can use an if-else statement where the conditional expression checks if there is a value in positional parameter $1. However, if there is a value, it echoes the number of input parameters and the first parameter using positional parameters.
#!/bin/bash
if [ -z "$1" ]
then
echo "Please, pass an argument"
exit 1
else
echo "The number of input arguments are"
echo $#
echo "The first one is"
echo $1
fi
Let's run the code without arguments:
$ ./script.sh
Output of the script:
Please, pass an argument
Now, let's run it with some parameters:
$ ./script.sh jiyik stack blog
Output of the code:
The number of input arguments are
3
The first one is
jiyik
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.
Related Articles
Run batch (.bat) files in CMD
Publish Date:2025/03/20 Views:155 Category:OPERATING SYSTEM
-
This article will show you how to use CMD to run a batch file.bat. There are three ways to run a batch file. Let us discuss them in the following sections. Run batch (.bat) files in CMD by directly clicking on them This way you can just go
Changing CMD text color using batch script
Publish Date:2025/03/20 Views:172 Category:OPERATING SYSTEM
-
This article will first discuss the basic concepts of batch scripts or batch files. After introducing the Batch script, let's discuss how to use the Batch script to change the text color of CMD every second. Batch script or file A batch scr
Batch set command timeout
Publish Date:2025/03/20 Views:50 Category:OPERATING SYSTEM
-
This article will first discuss the concept of timeout command in batch scripting. After that, we will discuss setting timeout command for any other command. Timeout command in batch script A timeout is a utility that pauses or delays for a
Batch merge XML files
Publish Date:2025/03/20 Views:101 Category:OPERATING SYSTEM
-
This article will first discuss and understand the XML file format. After that, we will discuss merging two or more XML files into one file using batch commands and scripts. XML File XML, also known as Extensible Markup Language, is a marku
Run .exe file from command prompt using batch script
Publish Date:2025/03/20 Views:77 Category:OPERATING SYSTEM
-
This article will show you how to use a batch (.bat) script to run a .exe type file. You can use two different commands to achieve this. Let’s discuss each method in the following sections. Run .exe file from command prompt using title an
Deleting files older than N days using batch script
Publish Date:2025/03/20 Views:57 Category:OPERATING SYSTEM
-
In this article, we will use a batch script to delete files older than N days. Deleting files older than N days using batch script The general format of the code to perform this task is shown below. FORFILES /p "D:\DIRECTORY" /S /M *.* /D -
Deleting files using batch script
Publish Date:2025/03/20 Views:178 Category:OPERATING SYSTEM
-
This article will demonstrate how to delete files using a batch script. Deleting files using batch script Generally, we can easily delete files by clicking on delete or pressing the delete button on the keyboard. But in Batch, we have to fo
Concatenate multiple files using batch script
Publish Date:2025/03/20 Views:195 Category:OPERATING SYSTEM
-
In this article, we will see how to concatenate multiple files into one file. Concatenate multiple files using batch script The general format of the commands we will use is shown below. type FileONE.txt FileTWO.txt ConcatFile.txt There are
Check if a file exists using batch processing
Publish Date:2025/03/20 Views:101 Category:OPERATING SYSTEM
-
This article will demonstrate how to use a batch script to check if a file exists through sample code. Check if a file exists using batch script The general format or syntax of the code to check if a file exists is provided below. IF EXIST