0

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;
fi

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.

  • 1
    What result did you get when you tested it? – ctrl-alt-delor Jan 10 at 11:25
2

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

By clicking "Post Your Answer", you agree to our terms of service, privacy policy and cookie policy

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