JIYIK CN >

Current Location:Home > Learning > OPERATING SYSTEM >

Check if input parameter exists in Bash

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

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 $1would 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.

Article URL:

Related Articles

How to decompress x.tar.xz format files under Linux

Publish Date:2025/04/08 Views:186 Category:OPERATING SYSTEM

A lot of software found today is in the tar.xz format, which is a lossless data compression file format that uses the LZMA compression algorithm. Like gzip and bzip2, it supports multiple file compression, but the convention is not to compr

Summary of vim common commands

Publish Date:2025/04/08 Views:115 Category:OPERATING SYSTEM

In Linux, the best editor should be vim. However, the complex commands behind vim's powerful functions also make us daunted. Of course, these commands do not need to be memorized by rote. As long as you practice using vim more, you can reme

Detailed explanation of command return value $? in Linux

Publish Date:2025/04/08 Views:58 Category:OPERATING SYSTEM

? is a special variable. This variable represents the return value of the previous command. That is to say, when we run certain commands, these commands will return a code after running. Generally, if the command is successfully run, the re

Common judgment formulas for Linux script shell

Publish Date:2025/04/08 Views:159 Category:OPERATING SYSTEM

In shell script programming, predicates are often used. There are two ways to use predicates, one is to use test, and the other is to use []. Let's take a look at how to use these two methods through two simple examples. Example 1 # test –

How to use the Linux file remote copy command scp

Publish Date:2025/04/08 Views:151 Category:OPERATING SYSTEM

Scp copies files between two hosts over the network, and the data is encrypted during transmission. Its underlying layer uses ssh for data transmission. And it has the same authentication mechanism and the same security level as ssh. When u

Scan to Read All Tech Tutorials

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

Recommended

Tags

Scan the Code
Easier Access Tutorial