How to use shell to retrieve the number of parameters

In a shell script, we can use the special variable $# to obtain the number of arguments passed to the script. Here is a simple example:

#!/bin/bash

echo "Number of arguments: $#"

In the example above, $# represents the number of parameters passed to the script. You can save this script as a file (such as count_args.sh) and execute the following command in the command line to view the number of parameters:

bash count_args.sh arg1 arg2 arg3

After executing the above command, the output will be:

Number of arguments: 3

This indicates that there are three parameters being passed to the script.

bannerAds