Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I pass more than 10 arguments in windows batch file or linux shell script

I need to pass more than 10 arguments to a single batch file (shell script) but after the 9th argument it will not work (it will take from beginning)

code sample

 echo Hello! This a sample batch file.
    echo %1 %2 %3 %4 %5 %6 %7 %8 %9 %10 %11 %12 %13 %14 %15 %16
    pause



>mybatchdotbat a b c d e f g h i j  k l m n o p

can anyone give solution for this

like image 882
smartechno Avatar asked Mar 13 '15 04:03

smartechno


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 be passed to a batch file?

There is no practical limit to the number of parameters you can pass to a batch file, but you can only address parameter 0 (%0 - The batch file name) through parameter 9 (%9).

Can same shell script file run on both Windows and Linux?

Or may be you write programs in Java and for each operating system you have a script to set up an environment (sh-script or cmd-script). If you need to do basically the same in Windows and in Linux, you can write only one script, that will work in both operating systems.

How can I pass arguments to a batch file from a source text file?

It can be solved with reading from a temporary file a remarked version of the parameter. @echo off SETLOCAL DisableDelayedExpansion SETLOCAL for %%a in (1) do ( set "prompt=" echo on for %%b in (1) do rem * #%1# @echo off ) > param. txt ENDLOCAL for /F "delims=" %%L in (param.


1 Answers

Basically, %10 is getting interpreted as %1 0.

To fix this, in either a batch file or a shell script, you can save the first argument in a variable, then use shift to decrement all remaining arguments by 1. When you call shift, %1 ($1 in a shell script) is now gone, the old %2 becomes %1, the old %3 becomes %2, etc. See this answer for more details.

In a shell script, you can also refer to the tenth argument using ${10}.

like image 101
fzzfzzfzz Avatar answered Sep 29 '22 05:09

fzzfzzfzz