Name

system

Synopsis

Executes a shell command

#include <stdlib.h>
intsystem( const char *s );

The system() function passes a command line addressed by the pointer argument s to an operating system shell. If s is a null pointer, the function returns true (a nonzero value) if a command processor is available to handle shell commands, and 0 or false if not.

How the system executes a command, and what value the system() function returns, are left up to the given implementation. The command may terminate the program that calls system(), or have unspecified effects on its further behavior.

Example

if ( system( NULL ))system( "echo \"Shell: $SHELL; process ID: $$\"");
else
  printf( "No command processor available.\n" );

This example is not portable, but on certain systems it can produce output like this:

Shell: /usr/local/bin/bash; process ID: 21349

See Also

getenv()

Get C in a Nutshell now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.