I have some understanding that to check the number of arguments being passed into the shell script all I have to do inside the shell script is this: Example 1

if [ "$#" -ne 2 ]; then
   do something;

However, I am a little confused and would like to know whether the name of the shell script when being run is considered part of the arguments as well as this is the case for python script when it comes to sys.argv.

An example in the terminal would be like: Example 2

./script.sh $1 $2

In this case for Example 2 how many arguments would actually be considered to be passed in to Example 1 if else loop.

New contributor
user3187113 is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
  • What result did you get when you tested it? – ctrl-alt-delor Jan 10 at 11:25

The name of the script is not considered as part of the positional parameters. This means that

somescript arg1 arg2

will set $1 to arg1 and $2 to arg2 and that $# will be 2.

The name of the script will be available in $0, but $0 is special in that it's not included in the array $@, and $# is the length (number of elements) of $@.

  • 1
    thank you! I understand now. – user3187113 Jan 10 at 16:25

Your Answer

user3187113 is a new contributor. Be nice, and check out our Code of Conduct.

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.