With first 9 arguments being referred from $1-$9, $10 gets interpreted as $1 followed by a 0. How do I account for this and access arguments to functions greater than 10?
Thanks.
If there are more than 9 arguments, then tenth or onwards arguments can't be assigned as $10 or $11. You have to either process or save the $1 parameter, then with the help of shift command drop parameter 1 and move all other arguments down by one. It will make $10 as $9, $9 as $8 and so on.
Except for functions with variable-length argument lists, the number of arguments in a function call must be the same as the number of parameters in the function definition. This number can be zero. The maximum number of arguments (and corresponding parameters) is 253 for a single function.
While a function can only have one argument of variable length of each type, we can combine both types of functions in one argument. If we do, we must ensure that positional arguments come before named arguments and that fixed arguments come before those of variable length.
5 Types of Arguments in Python Function Definition: keyword arguments. positional arguments. arbitrary positional arguments. arbitrary keyword arguments.
Use :
#!/bin/bash
echo ${10}
To test the difference with $10, code in foo.sh :
#!/bin/bash
echo $10
echo ${10}
Then :
$ ./foo.sh first 2 3 4 5 6 7 8 9 10
first0
10
the same thing is true if you have :
foobar=42
foo=FOO
echo $foobar # echoes 42
echo ${foo}bar # echoes FOObar
Use {}
when you want to remove ambiguities ...
my2c
If you are using bash, then you can use ${10}
.
${...} syntax seems to be POSIX-compliant in this particular case, but it might be preferable to use the command shift
like that :
while [ "$*" != "" ]; do echo "Arg: $1" shift done
EDIT: I noticed I didn't explain what shift
does. It just shift the arguments of the script (or function). Example:
> cat script.sh echo "$1" shift echo "$1" > ./script.sh "first arg" "second arg" first arg second arg
In case it can help, here is an example with getopt/shift :
while getopts a:bc OPT; do case "$OPT" in 'a') ADD=1 ADD_OPT="$OPTARG" ;; 'b') BULK=1 ;; 'c') CHECK=1 ;; esac done shift $( expr $OPTIND - 1 ) FILE="$1"
In general, to be safe that the whole of a given string is used for the variable name when Bash is interpreting the code, you need to enclose it in braces: ${10}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With