How to generate below command dynamically for n number of elements? I don't want to use any other variable or an array.
command mycommand /path/location ${arg[1]}="${value[1]}" ${arg[2]}="${value[2]}" ...
You can use a subshell with a for loop that loops over your arrays and creates the correct arguments.
command mycommand /path/location $(for ((i=0; i<"${#value[@]}"; i++)) do printf "%s=%s " "${arg[$i]}" "${value[$i]}"; done)
Like I promised in the comments, this will work even if there are spaces in the values.
Here I create the arguments dynamically and separate the arguments on null characters (\0).
I pipe these to xargs. xargs is some kind of argument parser and probably the best choice for the job.
So, instead of letting bash pass the argument (which passes them by breaking them on whitespace), we are letting xargs pass the argument and have full control on how they are splitted.
In this case I choose to separate on null characters (by setting -0), so that every other character (spaces, or even linebreaks) do not interfere with how the arguments are passed.
for ((i=0; i<"${#value[@]}"; i++)) do printf "%s=%s\0" "${arg[$i]}" "${value[$i]}"; done |
xargs -0 command mycommand /path/location
eval, or pipe the arguments through xargs with the --null option. You can take a look at it yourself and maybe find the solution yourself.
https://stackoverflow.com/q/54149440/3220113?