subbarao
subbarao

Reputation: 107

How to pass command line arguments from C program to the bash script?

I have written one bash script and now I am calling this script from C program. Now I want to pass arguments i.e. argv[1] and argv[2] to the script from command line.

Upvotes: 0

Views: 5641

Answers (1)

Stanislav
Stanislav

Reputation: 65

It depends on the way the script is called. For example, if you are using system you can preformat string that used to invoke bash script from system call adding command line arguments:

C

#include "stdio.h"

void main(int argc, char const *argv[])
{
    if (argc == 2) {
        char command[100] = {0};

        sprintf(command, "./example.sh %s", argv[1]);
        system(command);
    }
}

Bash

#!/bin/bash

echo $1

As a result

$ gcc example.c -o example && ./example Hello!
Hello!

Upvotes: 3

Related Questions