-3

I have some problems passing argument from bash file to C program. I'm using a bash script to compile C program like this:

gcc -O0 filec1.c -lm -o fileoutput
./fileoutput $1 $2 $3 $4 $5 $6 $7 $8 $9 $10 $11

Now if I print each argument in bash file adding these lines:

for i in $@; do
    echo $i
done

each argument passed is correctly printed but if I print each argument in the c file that has just a main with the following lines:

int main(int argc, char **argv){
    for(int i=0; i<argc; i++){
        printf("%s\n", argv[i]);
    }
}

the output for the arguments after 9th are wrong or not printed. Someone can help me please?

furgius
  • 17
  • 3
  • 1
    Please include a [minimal, reproducible example](https://stackoverflow.com/help/minimal-reproducible-example) of your problem. As your question stands right now, it's close to impossible to know what's actually wrong here. – Frxstrem Jun 01 '19 at 12:52
  • 2
    Also, can you explain clearly what you think is wrong and what you expect it to do. Just saying "it is wrong" can really only result in the reply that "it is just doing what you told it to do". It is frustrating because you know what you want to do, but can not enter that into the machine - it can not read your mind. Unfortunately, we can not read your mind either, so you need to clearly state the issue. Also, FWIW, it is unusual to run a command from the command line and pass it parameters such as $1 $2 and so on. *Normally* these "parameters" only have meaning when inside a bash script. – GMc Jun 01 '19 at 12:58
  • 1
    What does the C program look like? How are you processing the arguments? – John Bode Jun 01 '19 at 13:03
  • Possible duplicate of [pass arguments between shell scripts but retain quotes](https://stackoverflow.com/questions/1987162/pass-arguments-between-shell-scripts-but-retain-quotes) – Daniel Pryden Jun 01 '19 at 13:14
  • 1
    Numbered parameters above 9 require braces, for example `${10} ${11} ${12}` etc. If you don't use braces it will use the single digit parameter followed by a literal number. for example `$12` will give the value of `$1` followed by a literal `2`. – cdarke Jun 01 '19 at 14:54

1 Answers1

1

If you are referring to bash arguments inside a bash script, you can use "$@" instead of individual arguments, it expands to whatever arguments you pass to it.

Edit

https://stackoverflow.com/a/3898681/152016 refer to the bash man page, explaining the meaning of that special variable, there are other useful variables too like $# and $?.

Added quotes around the variable thanks to @DanielPryden comment. See a very good explanation why, here: https://stackoverflow.com/a/3990540/152016

Niloct
  • 9,491
  • 3
  • 44
  • 57
  • @DanielPryden is it necessary ? – Niloct Jun 01 '19 at 13:11
  • For the downvoter I've edited the answer. – Niloct Jun 01 '19 at 13:13
  • 1
    @Niloct: Yes, the quotes are necessary if you want to preserve whitespace and special characters in the arguments. – Daniel Pryden Jun 01 '19 at 13:15
  • @DanielPryden I've tried the code in the last link of my answer but getting different results. When you don't provide arguments with quotes to the script, the special characters aren't preserved with/without quotes in `$@`. When you do provide arguments with quotes, the behavior is the same either for `"$@"` and `$@`. Are you aware of a testing case that shows the difference ? Thanks. – Niloct Jun 01 '19 at 13:45
  • 1
    @Niloct: It sounds like you're trying to ask a new question. In that case, go ahead and ask a question. But I will just emphasize that you need to understand that the actual values that are being passed to the child process (as `argv` in C) are different from what you write on the command line. If you use quotes in the shell, that just affects how the shell parses the string into an array of strings; the quote characters themselves are not included in the strings passed to the child process. – Daniel Pryden Jun 01 '19 at 14:13
  • I've finally put up an example integrated with a simple C program that shows the difference. – Niloct Jun 01 '19 at 14:51
  • Using `set -x` helps to see what bash does with quoted arguments. – Niloct Jun 01 '19 at 14:52