Find the answer to your Linux question:
Results 1 to 3 of 3
How can I execute several commands in one system() call? For example: system("echo -n 'hey'; ls -l /tmp; echo -n 'bye'"); I tried ";", " ", "," and "|" but ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Apr 2005
    Posts
    5

    C: system()


    How can I execute several commands in one system() call?

    For example: system("echo -n 'hey'; ls -l /tmp; echo -n 'bye'");

    I tried ";", " ", "," and "|" but it always prints the whole string.

  2. #2
    Just Joined!
    Join Date
    Jul 2004
    Posts
    94
    Doesn't && do it for you?

  3. #3
    Linux Guru
    Join Date
    Oct 2001
    Location
    Täby, Sweden
    Posts
    7,578
    I'm not sure that I understand your problem. When I compile that exact line that you gave and run the resulting program, I get "hey", followed by a listing of /tmp, followed by "bye". Is that not what you want?

    Code:
    #include <stdlib.h>
    #include <unistd.h>
    #include <stdio.h>
    
    int main(void)
    {
        system("echo -n 'hey'; ls -l /tmp; echo -n 'bye'");
        return(0);
    }

  4. $spacer_open
    $spacer_close

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •