There is this code:
test2.cpp (compiled to test2 executable):
#include <iostream>
int main(int argc, char** argv)
{
for(int i = 0; i < argc; ++i){
std::cout << i << " " << argv[i] << std::endl;
}
return 0;
}
test.sh (parameters preparing script):
some_array=("text1" "text two" "text tree")
input_variables=""
for i in $(seq 0 $((${#some_array[*]} - 1))); do
input_variables="$input_variables --parameter=\"${some_array[$i]}\""
echo $input_variables
done
echo "Running: ./test2$input_variables"
./test2 $input_variables
Output:
$ sh test.sh
--parameter="text1"
--parameter="text1" --parameter="text two"
--parameter="text1" --parameter="text two" --parameter="text tree"
Running: ./test2 --parameter="text1" --parameter="text two" --parameter="text tree"
0 ./test2
1 --parameter="text1"
2 --parameter="text
3 two"
4 --parameter="text
5 tree"
I would like to pass to test2 executable 3 arguments but 5 arguments are passed and there is a problem with space character between words. I would like also to keep input parameters in array like it is presented in *some_array* so I can add there something whenever I want. How to pass these parameters correctly?