I start programming in c language, and I ask myself this question.
I have a file lol.c :
#include <stdio.h>
#include <stdlib.h>
int main (int argc, char *argv[]) {
printf ("argc\t= %d\n", argc);
for (int i=0; i < argc; i++)
printf ("argv[%i]\t= %s\n", i, argv[i]);
return 0;
}
So when I execute the file (after compilation) :
./lol "abc" "def"
He returns so :
argc = 3
argv[0] = ./lol
argv[1] = abc
argv[2] = def
Now, I have a bash script lol.sh :
#!/bin/bash
./lol "abc"
When I execute the file :
bash lol.sh def
So we get :
argc = 2
argv[0] = ./lol
argv[1] = abc
How to retrieve the positional parameters passed to the bash script (from the file lol.c) ?
My first solution is to pass the positional parameters to the lol command like this (lol.sh) :
#!/bin/bash
./lol "abc" "$*"
He returns so :
argc = 3
argv[0] = ./lol
argv[1] = abc
argv[2] = def
I am not very satisfied with this solution, is there another way for that ?
Thanks