Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to execute bash commands from C? [duplicate]

Tags:

c

bash

gzip

Is there a way to run command line utilities, e.g. gzip, into a C app?

like image 521
tarabyte Avatar asked Apr 20 '15 23:04

tarabyte


People also ask

What is CTRL C in bash?

When you hit Ctrl + c , the line discipline of your terminal sends SIGINT to processes in the foreground process group. Bash, when job control is disabled, runs everything in the same process group as the bash process itself. Job control is disabled by default when Bash interprets a script.


1 Answers

Use system():

#include <stdlib.h>
int status = system("gzip foo");

See the man page (man 3 system) for more detailed information on how to use it.

By the way, this question already has an answer here: How do I execute external program within C code in linux with arguments?

like image 147
jayhendren Avatar answered Oct 28 '22 23:10

jayhendren