How to Handle More Than 10 Parameters in Shell

How to handle more than 10 parameters in shell

Use curly braces to set them off:

echo "${10}"

Any positional parameter can be saved in a variable to document its use and make later statements more readable:

city_name=${10}

If fewer parameters are passed then the value at the later positions will be unset.

You can also iterate over the positional parameters like this:

for arg

or

for arg in "$@"

or

while (( $# > 0 ))    # or [ $# -gt 0 ]
do
echo "$1"
shift
done

How to Pass More then 10 argument in ShellScript

Here is an example :

#!/bin/ksh   
echo There is $# parameters
while [ $# -ne 0 ]; do
echo $1
shift
done

The output :

     Will:/home> ./script a b c d e f g h i j k l m n o p q r s t u v w x y z
There is 26 parameters
a
b
c
d
e
f
g
h
i
j
k
l
m
n
o
p
q
r
s
t
u
v
w
x
y
z

$#count the number of parameters passed to your script.

Shift is moving the param to the left so after one loop $1 become b and so on.

How do I access arguments to functions if there are more than 9 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

Why do bash command line arguments after 9 require curly brackets?

Specifically, your question relates to "positional parameters." Using $var instead of ${var} is shorthand in bash. In most cases it works well. Bash variables must start with a letter or underscore. It internally treats variables that start with a digit as a "positional parameter." When bash detects a positional parameter it only looks at the first digit, which is why $10 returns $1"0". By calling ${10} you are instructing bash to look at the complete variable instead of its built-in default of the first digit.

As to why it is this way? I have no idea. Legacy implementation which has been expanded upon is my guess. "Who would ever need more than....?"

Bash script which will take 10 command line arguments and print it

#!/bin/bash

echo $1 $2 $3 $4 $5 $6 $7 $8 $9 ${10}

Linux shell dialog menu having more than 10 items problem

Dialog --menu works like

dialog --menu text height width menu-height tag1 item1 tag2 item2 tag3 item3 etc...

Each item has a tag, see man dialog. You need for example insert numbers in front of each array element.

my_array2=($(
i=1
for j in "${my_array[@]}"; do
echo "$i"
echo "$j"
i=$((i+1))
done
))
dialog ... --menu ... "${my_array2[@]}"

How to pass more than 9 parameters to batch file

save the first nine args in a variable. THEN call shift multiple times and only then use the rest:

set "v=http://example.com?firstName=%1&middleName=%2&lastName=%3&country=%4&address=%5&address2=%6&address3=%7&mobileNo=%8&landlineNo=%9"
shift
shift
shift
shift
shift
shift
shift
shift
shift
start iexplore %v%&emailAddress=%1&hobby1=%2&hobby2=%3&hobby3=%4&hobby4=%5&hobby5=%6

How do you utilize more than 9 arguments when calling a label in a CMD batch-script?

Use the shift command if you want to work with more than 9 parameters.

(actually more than 10 parameters if you count the %0 parameter)

You can [...] use the shift command to create a batch file that can accept more than 10 batch parameters. If you specify more than 10 parameters on the command line, those that appear after the tenth (%9) will be shifted one at a time into %9.

You can either use a loop, store the variables before shifting, or do it quick like this:

@echo off
CALL:LABEL "one" "two" "three" "four" "five" "six" "seven" "eight" "nine" "ten" "eleven" "twelve"
PAUSE
GOTO:EOF

:LABEL
:: print arguments 1-9
echo %1
echo %2
echo %3
echo %4
echo %5
echo %6
echo %7
echo %8
echo %9

:: print arguments 10-11
shift
shift
echo %8
echo %9

:: print argument 13
shift
echo %9

You can replace the shift commands with a loop in case you have many arguments. The following for loop executes shift nine times, so that %1 will be the tenth argument.

@for /L %%i in (0,1,8) do shift


Related Topics



Leave a reply



Submit