Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I access arguments to functions if there are more than 9 arguments?

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.

like image 548
fzkl Avatar asked Jan 27 '11 13:01

fzkl


People also ask

How do I pass more than 9 arguments in shell script?

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.

How many arguments can a function have?

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.

How many arguments can be passed to a Python 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.

How many arguments can be passed to a Python function None 12 any?

5 Types of Arguments in Python Function Definition: keyword arguments. positional arguments. arbitrary positional arguments. arbitrary keyword arguments.


3 Answers

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

like image 187
neuro Avatar answered Sep 22 '22 17:09

neuro


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"
like image 33
Chl Avatar answered Sep 24 '22 17:09

Chl


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}

like image 31
l0b0 Avatar answered Sep 22 '22 17:09

l0b0